Home >Backend Development >PHP Tutorial >Basic use of PHP namespaces
namespace FooBarsubnamespace;
const FOO = 1;
function foo() {}
class foo{
static function staticmethod() {}
}
?>
namespace FooBar;
include 'file1.php';
const FOO = 2;
function foo() {}
class foo{
static function staticmethod() {}
}
foo(); // Resolves to FooBarfoo resolves to function FooBarfoo
foo::staticmethod(); // Resolves to the static method staticmethod of class FooBarfoo. resolves to class FooBarfoo, method staticmethod
echo FOO; // resolves to constant FooBarFOO
subnamespacefoo(); // Resolved as function FooBarsubnamespacefoo
subnamespacefoo::staticmethod(); // Resolved as class FooBarsubnamespacefoo, and class method staticmethod
echo subnamespaceFOO; // Resolved as constant FooBarsubnamespaceFOO
FooBarfoo(); // Resolved as function FooBarfoo
FooBarfoo::staticmethod(); // Resolved as class FooBarfoo, and class method staticmethod
echo FooBarFOO; // Resolved as constant FooBarFOO
?>
namespace FooBarsubnamespace;
const FOO = 1;
function foo() {}
class foo{
static function staticmethod() {}
}
?>
namespace FooBar;
include 'file1.php';
const FOO = 2;
function foo() {}
class foo{
static function staticmethod() {}
}
foo(); // Resolves to FooBarfoo resolves to function FooBarfoo
foo::staticmethod(); // Resolves to the static method staticmethod of class FooBarfoo. resolves to class FooBarfoo, method staticmethod
echo FOO; // resolves to constant FooBarFOO
subnamespacefoo(); // Resolved as function FooBarsubnamespacefoo
subnamespacefoo::staticmethod(); // Resolved as class FooBarsubnamespacefoo, and class method staticmethod
echo subnamespaceFOO; // Resolved as constant FooBarsubnamespaceFOO
FooBarfoo(); // Resolved as function FooBarfoo
FooBarfoo::staticmethod(); // Resolved as class FooBarfoo, and class method staticmethod
echo FooBarFOO; // Resolved as constant FooBarFOO
?>