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

Basic use of PHP namespaces

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-03-01 17:02:01
Original
933 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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template