Home >Backend Development >PHP Tutorial >Basic use of PHP namespaces

Basic use of PHP namespaces

WBOY
WBOYOriginal
2016-12-01 00:25:36900browse

file1.php:

namespace FooBarsubnamespace;

const FOO = 1;
function foo() {}
class foo{
static function staticmethod() {}
}
?>

file2.php:

namespace FooBar;
include 'file1.php';

const FOO = 2;
function foo() {}
class foo{
static function staticmethod() {}
}

Unqualified name

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

Qualified name

subnamespacefoo(); // Resolved as function FooBarsubnamespacefoo
subnamespacefoo::staticmethod(); // Resolved as class FooBarsubnamespacefoo, and class method staticmethod
echo subnamespaceFOO; // Resolved as constant FooBarsubnamespaceFOO

Fully qualified name

FooBarfoo(); // Resolved as function FooBarfoo
FooBarfoo::staticmethod(); // Resolved as class FooBarfoo, and class method staticmethod
echo FooBarFOO; // Resolved as constant FooBarFOO
?>

Reply content:

file1.php:

namespace FooBarsubnamespace;

const FOO = 1;
function foo() {}
class foo{
static function staticmethod() {}
}
?>

file2.php:

namespace FooBar;
include 'file1.php';

const FOO = 2;
function foo() {}
class foo{
static function staticmethod() {}
}

Unqualified name

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

Qualified name

subnamespacefoo(); // Resolved as function FooBarsubnamespacefoo
subnamespacefoo::staticmethod(); // Resolved as class FooBarsubnamespacefoo, and class method staticmethod
echo subnamespaceFOO; // Resolved as constant FooBarsubnamespaceFOO

Fully qualified name

FooBarfoo(); // Resolved as function FooBarfoo
FooBarfoo::staticmethod(); // Resolved as class FooBarfoo, and class method staticmethod
echo FooBarFOO; // Resolved as constant FooBarFOO
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn