Using namespace in class php
PHP supports two ways of abstractly accessing elements within the current namespace, the __NAMESPACE__ magic constant, and the namespace keyword.
The value of __NAMESPACE__ is a string that contains the current namespace name. In global, un-namespaced code, it contains an empty string.
Example #1 __NAMESPACE__ example, namespaced code
echo ‘»‘ , __NAMESPACE__ , ‘»‘ ; // outputs «MyProject»
?>
Example #2 __NAMESPACE__ example, global code
echo ‘»‘ , __NAMESPACE__ , ‘»‘ ; // outputs «»
?>
The __NAMESPACE__ constant is useful for dynamically constructing names, for instance:
Example #3 using __NAMESPACE__ for dynamic name construction
function get ( $classname )
$a = __NAMESPACE__ . ‘\\’ . $classname ;
return new $a ;
>
?>
The namespace keyword can be used to explicitly request an element from the current namespace or a sub-namespace. It is the namespace equivalent of the self operator for classes.
Example #4 the namespace operator, inside a namespace
use blah \ blah as mine ; // see «Using namespaces: Aliasing/Importing»
blah \ mine (); // calls function MyProject\blah\mine()
namespace\ blah \ mine (); // calls function MyProject\blah\mine()
namespace\ func (); // calls function MyProject\func()
namespace\ sub \ func (); // calls function MyProject\sub\func()
namespace\ cname :: method (); // calls static method «method» of class MyProject\cname
$a = new namespace\ sub \ cname (); // instantiates object of class MyProject\sub\cname
$b = namespace\ CONSTANT ; // assigns value of constant MyProject\CONSTANT to $b
?>
Example #5 the namespace operator, in global code
namespace\ func (); // calls function func()
namespace\ sub \ func (); // calls function sub\func()
namespace\ cname :: method (); // calls static method «method» of class cname
$a = new namespace\ sub \ cname (); // instantiates object of class sub\cname
$b = namespace\ CONSTANT ; // assigns value of constant CONSTANT to $b
?>
Namespaces
The keyword ‘use’ has two different applications, but the reserved word table links to here.
It can apply to namespace constucts:
file1:
class Cat <
static function says () > ?>
file2:
class Dog static function says () > ?>
file3:
class Animal static function breathes () > ?>
file4:
include ‘file1.php’ ;
include ‘file2.php’ ;
include ‘file3.php’ ;
use foo as feline ;
use bar as canine ;
use animate ;
echo \ feline \ Cat :: says (), «
\n» ;
echo \ canine \ Dog :: says (), «
\n» ;
echo \ animate \ Animal :: breathes (), «
\n» ; ?>
Note that
felineCat::says()
should be
\feline\Cat::says()
(and similar for the others)
but this comment form deletes the backslash (why. )
The ‘use’ keyword also applies to closure constructs:
$callback =
function ( $pricePerItem ) use ( $tax , & $total )
$total += $pricePerItem * ( $tax + 1.0 );
>;
array_walk ( $products_costs , $callback );
return round ( $total , 2 );
>
?>
Tested on PHP 7.0.5, Windows
The line «use animate;» equals the line «use animate as animate;»
but the «use other\animate;» equals «use other\animate as animate;»
file1:
class Cat <
static function says () > ?>
file2:
class Dog static function says () > ?>
file3:
class Animal static function breathes () > ?>
file4:
include ‘file1.php’ ;
include ‘file2.php’ ;
include ‘file3.php’ ;
use foo as feline ;
use bar as canine ;
use other \ animate ; //use other\animate as animate;
echo feline \ Cat :: says (), «
\n» ;
echo canine \ Dog :: says (), «
\n» ;
echo \ animate \ Animal :: breathes (), «
\n» ; ?>
In addition to using namespaces and closures, the use keyword has another new meaning as of PHP 5.4 — using traits:
trait Hello public function sayHello () echo ‘Hello ‘ ;
>
>
trait World public function sayWorld () echo ‘World’ ;
>
>
class MyHelloWorld use Hello , World ;
public function sayExclamationMark () echo ‘!’ ;
>
>
Using namespace in class php
- Relative file name like foo.txt . This resolves to currentdirectory/foo.txt where currentdirectory is the directory currently occupied. So if the current directory is /home/foo , the name resolves to /home/foo/foo.txt .
- Relative path name like subdirectory/foo.txt . This resolves to currentdirectory/subdirectory/foo.txt .
- Absolute path name like /main/foo.txt . This resolves to /main/foo.txt .
- Unqualified name, or an unprefixed class name like $a = new foo(); or foo::staticmethod(); . If the current namespace is currentnamespace , this resolves to currentnamespace\foo . If the code is global, non-namespaced code, this resolves to foo . One caveat: unqualified names for functions and constants will resolve to global functions and constants if the namespaced function or constant is not defined. See Using namespaces: fallback to global function/constant for details.
- Qualified name, or a prefixed class name like $a = new subnamespace\foo(); or subnamespace\foo::staticmethod(); . If the current namespace is currentnamespace , this resolves to currentnamespace\subnamespace\foo . If the code is global, non-namespaced code, this resolves to subnamespace\foo .
- Fully qualified name, or a prefixed name with global prefix operator like $a = new \currentnamespace\foo(); or \currentnamespace\foo::staticmethod(); . This always resolves to the literal name specified in the code, currentnamespace\foo .
Here is an example of the three kinds of syntax in actual code:
namespace Foo \ Bar \ subnamespace ;
?php
const FOO = 1 ;
function foo () <>
class foo
static function staticmethod () <>
>
?>
namespace Foo \ Bar ;
include ‘file1.php’ ;
?php
const FOO = 2 ;
function foo () <>
class foo
static function staticmethod () <>
>
/* Unqualified name */
foo (); // resolves to function Foo\Bar\foo
foo :: staticmethod (); // resolves to class Foo\Bar\foo, method staticmethod
echo FOO ; // resolves to constant Foo\Bar\FOO
/* Qualified name */
subnamespace \ foo (); // resolves to function Foo\Bar\subnamespace\foo
subnamespace \ foo :: staticmethod (); // resolves to class Foo\Bar\subnamespace\foo,
// method staticmethod
echo subnamespace \ FOO ; // resolves to constant Foo\Bar\subnamespace\FOO
/* Fully qualified name */
\ Foo \ Bar \ foo (); // resolves to function Foo\Bar\foo
\ Foo \ Bar \ foo :: staticmethod (); // resolves to class Foo\Bar\foo, method staticmethod
echo \ Foo \ Bar \ FOO ; // resolves to constant Foo\Bar\FOO
?>
Note that to access any global class, function or constant, a fully qualified name can be used, such as \strlen() or \Exception or \INI_ALL .
Example #1 Accessing global classes, functions and constants from within a namespace
function strlen () <>
const INI_ALL = 3 ;
class Exception <>
$a = \ strlen ( ‘hi’ ); // calls global function strlen
$b = \ INI_ALL ; // accesses global constant INI_ALL
$c = new \ Exception ( ‘error’ ); // instantiates global class Exception
?>