Basic use of PHP namespaces

WBOY
Release: 2023-03-01 17:02:01
Original
877 people have browsed it

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
?>

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!