note 114391 added to language.namespaces.rules
| From: | CJTaylor | Date: | Fri, 14 Feb 2014 21:52:02 +0000 |
| Subject: | note 114391 added to language.namespaces.rules | ||
| Groups: | php.notes php.notes | ||
| Request: | Send a blank email to php-notes+get-198515@lists.php.net to get a copy of this message | ||
It took me playing with it a bit as I had a hard time finding documentation on when a class name
matches a namespace, if that's even legal and what behavior to expect. It IS explained in #6
but I thought I'd share this with other souls like me that see it better by example. Assume
all 3 files below are in the same directory.
file1.php
<?php
namespace foo;
class foo {
static function hello() {
echo "hello world!";
}
}
?>
file2.php
<?php
namespace foo;
include('file1.php');
foo::hello(); //you're in the same namespace, or scope.
\foo\foo::hello(); //called on a global scope.
?>
file3.php
<?php
include('file1.php');
foo\foo::hello(); //you're outside of the namespace
\foo\foo::hello(); //called on a global scope.
?>
Depending upon what you're building (example: a module, plugin, or package on a larger
application), sometimes declaring a class that matches a namespace makes sense or may even be
required. Just be aware that if you try to reference any class that shares the same namespace, omit
the namespace unless you do it globally like the examples above.
I hope this is useful, particularly for those that are trying to wrap your head around this 5.3
feature.
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/language.namespaces.rules.php
Edit -- https://master.php.net/note/edit/114391
Del: integrated -- https://master.php.net/note/delete/114391/integrated
Del: useless -- https://master.php.net/note/delete/114391/useless
Del: bad code -- https://master.php.net/note/delete/114391/bad+code
Del: spam -- https://master.php.net/note/delete/114391/spam
Del: non-english -- https://master.php.net/note/delete/114391/non-english
Del: in docs -- https://master.php.net/note/delete/114391/in+docs
Del: other reasons-- https://master.php.net/note/delete/114391
Reject -- https://master.php.net/note/reject/114391
Search -- https://master.php.net/manage/user-notes.php