diff --git a/language/namespaces.xml b/language/namespaces.xml index e57a88c1dbce..98f35945224b 100644 --- a/language/namespaces.xml +++ b/language/namespaces.xml @@ -1,7 +1,7 @@ + version="1.1" annotations="interactive"> Namespaces @@ -58,16 +58,19 @@ function myfunction() {} const MYCONST = 1; $a = new MyClass; +print $a::class . "\n"; $c = new \my\name\MyClass; // see "Global Space" section +print $c::class . "\n"; $a = strlen('hi'); // see "Using namespaces: fallback to global // function/constant" section +print $a . "\n"; $d = namespace\MYCONST; // see "namespace operator and __NAMESPACE__ // constant" section +print $d . "\n"; $d = __NAMESPACE__ . '\MYCONST'; echo constant($d); // see "Namespaces and dynamic language features" section -?> ]]> @@ -101,7 +104,7 @@ echo constant($d); // see "Namespaces and dynamic language features" section keyword. Declaring a single namespace - + ]]> @@ -125,12 +126,11 @@ function connect() { /* ... */ } no non-PHP code may precede a namespace declaration, including extra whitespace: Declaring a single namespace - + ]]> @@ -150,7 +150,7 @@ namespace MyProject; // fatal error - namespace must be the first statement in t sub-levels: Declaring a single namespace with hierarchy - + ]]> @@ -179,7 +177,7 @@ function connect() { /* ... */ } Declaring multiple namespaces, simple combination syntax - + ]]> @@ -205,7 +202,7 @@ function connect() { /* ... */ } Declaring multiple namespaces, bracketed syntax - + ]]> @@ -243,16 +239,22 @@ function connect() { /* ... */ } namespace MyProject { const CONNECT_OK = 1; -class Connection { /* ... */ } -function connect() { /* ... */ } +class Connection { + public static function start() { + print __METHOD__ . "\n"; + } +} + +function connect() { + print __FUNCTION__ . "\n"; +} } namespace { // global code -session_start(); -$a = MyProject\connect(); -echo MyProject\Connection::start(); +print strlen("hi") . "\n"; +MyProject\connect(); +MyProject\Connection::start(); } -?> ]]> @@ -265,20 +267,26 @@ echo MyProject\Connection::start(); ]]> @@ -358,7 +366,7 @@ echo MyProject\Connection::start(); Here is an example of the three kinds of syntax in actual code: file1.php - + ]]> file2.php - + ]]> @@ -422,10 +428,10 @@ function strlen() {} const INI_ALL = 3; class Exception {} -$a = \strlen('hi'); // calls global function strlen -$b = \INI_ALL; // accesses global constant INI_ALL +print \strlen('hi') . "\n"; // calls global function strlen +print \INI_ALL . "\n"; // accesses global constant INI_ALL $c = new \Exception('error'); // instantiates global class Exception -?> +print $c::class . "\n"; ]]> @@ -462,7 +468,6 @@ $obj = new $a; // prints classname::__construct $b = 'funcname'; $b(); // prints funcname echo constant('constname'), "\n"; // prints global -?> ]]> @@ -500,7 +505,6 @@ $b = '\namespacename\funcname'; $b(); // also prints namespacename\funcname echo constant('\namespacename\constname'), "\n"; // prints namespaced echo constant('namespacename\constname'), "\n"; // also prints namespaced -?> ]]> @@ -530,7 +534,6 @@ echo constant('namespacename\constname'), "\n"; // also prints namespaced namespace MyProject; echo '"', __NAMESPACE__, '"'; // outputs "MyProject" -?> ]]> @@ -539,9 +542,7 @@ echo '"', __NAMESPACE__, '"'; // outputs "MyProject" ]]> @@ -549,7 +550,7 @@ echo '"', __NAMESPACE__, '"'; // outputs "" names, for instance: using __NAMESPACE__ for dynamic name construction - + ]]> @@ -570,7 +570,7 @@ function get($classname) equivalent of the self operator for classes. the namespace operator, inside a namespace - + ]]> the namespace operator, in global code - + ]]> @@ -624,7 +622,7 @@ $b = namespace\CONSTANT; // assigns value of constant CONSTANT to $b Here is an example showing all 5 kinds of importing: importing/aliasing with the use operator - + ]]> @@ -667,14 +664,13 @@ echo CONSTANT; // echoes the value of My\Full\CONSTANT on the same line importing/aliasing with the use operator, multiple use statements combined - + ]]> @@ -684,7 +680,7 @@ NSname\subns\func(); // calls function My\Full\NSname\subns\func or constant names. Importing and dynamic names - + ]]> @@ -702,7 +697,7 @@ $obj = new $a; // instantiates object of class Another names are absolute, and unaffected by imports. Importing and fully qualified names - + ]]> @@ -740,7 +734,6 @@ function toGreenlandic() // ... } -?> ]]> @@ -760,7 +753,7 @@ function toGreenlandic() statement. - + Using global space specification - + ]]> @@ -832,10 +824,11 @@ namespace A\B\C; class Exception extends \Exception {} $a = new Exception('hi'); // $a is an object of class A\B\C\Exception +var_dump($a::class); $b = new \Exception('hi'); // $b is an object of class Exception +var_dump($b::class); $c = new ArrayObject; // fatal error, class A\B\C\ArrayObject not found -?> ]]> @@ -865,7 +858,6 @@ if (is_array('hi')) { // prints "is not array" } else { echo "is not array\n"; } -?> ]]> @@ -994,7 +986,7 @@ if (is_array('hi')) { // prints "is not array" Name resolutions illustrated - + ]]> @@ -1167,7 +1158,7 @@ A\B::foo(); // calls method "foo" of class "B" from namespace "A\A" +print $a::class; ]]> @@ -1182,7 +1173,7 @@ $a = new \stdClass; +print $a::class; ]]> @@ -1198,14 +1189,16 @@ $a = new stdClass; +print MyException::class . "\n"; ]]> @@ -1227,19 +1220,21 @@ namespace foo; class MyClass {} // using a class from the current namespace as a parameter type -function test(MyClass $parameter_type_example = null) {} +function test(?MyClass $parameter_type_example = null) {} // another way to use a class from the current namespace as a parameter type -function test(\foo\MyClass $parameter_type_example = null) {} +function test2(?\foo\MyClass $parameter_type_example = null) {} // extending a class from the current namespace class Extended extends MyClass {} +print Extended::class . "\n"; // accessing a global function -$a = \globalfunc(); +$a = \strlen("test"); +print $a . "\n"; // accessing a global constant $b = \INI_ALL; -?> +print $b . "\n"; ]]> @@ -1256,14 +1251,13 @@ $b = \INI_ALL; and \Exception is Exception. Fully Qualified names - + ]]> @@ -1286,7 +1280,7 @@ $a = \INI_ALL; // $a is set to the value of constant "INI_ALL" Qualified names - + ]]> @@ -1319,7 +1312,7 @@ $a = my\BAR; // sets $a to the value of constant "foo\my\BAR" Unqualified class names - + ]]> @@ -1361,8 +1353,12 @@ use blah\blah as foo; const FOO = 1; -function my() {} -function foo() {} +function my() { + print __FUNCTION__ . "\n"; +} +function foo() { + print __FUNCTION__ . "\n"; +} function sort(&$a) { \sort($a); // calls the global function "sort" @@ -1371,14 +1367,16 @@ function sort(&$a) } my(); // calls "foo\my" -$a = strlen('hi'); // calls global function "strlen" because "foo\strlen" does not exist +print strlen('hi') . "\n"; // calls global function "strlen" because "foo\strlen" does not exist $arr = array(1,3,2); $b = sort($arr); // calls function "foo\sort" -$c = foo(); // calls function "foo\foo" - import is not applied +var_dump($b); +foo(); // calls function "foo\foo" - import is not applied $a = FOO; // sets $a to value of constant "foo\FOO" - import is not applied +print $a; $b = INI_ALL; // sets $b to value of global constant "INI_ALL" -?> +print $b; ]]> @@ -1390,25 +1388,23 @@ $b = INI_ALL; // sets $b to value of global constant "INI_ALL" The following script combinations are legal: file1.php - + ]]> another.php - + ]]> file2.php - + ]]> @@ -1428,14 +1423,13 @@ $a = new MyClass; // instantiates class "thing" from namespace another in a separate file. However, the next example causes a fatal error on name conflict because MyClass is defined in the same file as the use statement. - + ]]> @@ -1454,7 +1448,6 @@ namespace my\stuff { class foo {} } } -?> ]]> @@ -1466,7 +1459,6 @@ namespace my\stuff { namespace my\stuff\nested { class foo {} } -?> ]]> @@ -1481,7 +1473,7 @@ namespace my\stuff\nested { there is a risk of unintended consequences: Dangers of using namespaced names inside a double-quoted string - + ]]> @@ -1506,7 +1497,7 @@ $obj = new $a; backslash will produce a fatal error if not found. Undefined constants - + ]]> @@ -1534,7 +1524,6 @@ namespace bar; const NULL = 0; // fatal error; const true = 'stupid'; // also fatal error; // etc. -?> ]]>