array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'language.namespaces.fallback.php', 1 => 'Fallback to global space', 2 => 'Using namespaces: fallback to the global space for functions and constants', ), 'up' => array ( 0 => 'language.namespaces.php', 1 => 'Namespaces', ), 'prev' => array ( 0 => 'language.namespaces.global.php', 1 => 'Global space', ), 'next' => array ( 0 => 'language.namespaces.rules.php', 1 => 'Name resolution rules', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'language/namespaces.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

Using namespaces: fallback to the global space for functions and constants

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

Inside a namespace, when PHP encounters an unqualified Name in a class name, function or constant context, it resolves these with different priorities. Class names always resolve to the current namespace name. Thus to access internal or non-namespaced user classes, one must refer to them with their fully qualified Name as in:

Приклад #1 Accessing global classes inside a namespace

<?php
namespace A\B\C;
class
Exception extends \Exception {}

$a = new Exception('hi'); // $a is an object of class A\B\C\Exception
$b = new \Exception('hi'); // $b is an object of class Exception

$c = new ArrayObject; // fatal error, class A\B\C\ArrayObject not found
?>

For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist.

Приклад #2 global functions/constants fallback inside a namespace

<?php
namespace A\B\C;

const
E_ERROR = 45;
function
strlen($str)
{
return
\strlen($str) - 1;
}

echo
E_ERROR, "\n"; // prints "45"
echo INI_ALL, "\n"; // prints "7" - falls back to global INI_ALL

echo strlen('hi'), "\n"; // prints "1"
if (is_array('hi')) { // prints "is not array"
echo "is array\n";
} else {
echo
"is not array\n";
}
?>