note 92608 added to language.namespaces.rules
| From: | rangel at osu1 dot php dot net | Date: | Fri, 31 Jul 2009 07:48:15 +0000 |
| Subject: | note 92608 added to language.namespaces.rules | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158943@lists.php.net to get a copy of this message | ||
The term "autoload" mentioned here shall not be confused with __autoload function to
autoload objects. Regarding the __autoload and namespaces' resolution I'd like to share
the following experience:
->Say you have the following directory structure:
- root
| - loader.php
| - ns
| - foo.php
->foo.php
<?php
namespace ns;
class foo
{
public $say;
public function __construct()
{
$this->say = "bar";
}
}
?>
-> loader.php
<?php
//GLOBAL SPACE <--
function __autoload($c)
{
require_once $c . ".php";
}
class foo extends ns\foo // ns\foo is loaded here
{
public function __construct()
{
parent::__construct();
echo "<br />foo" . $this->say;
}
}
$a = new ns\foo(); // ns\foo also loads ns/foo.php just fine here.
echo $a->say; // prints bar as expected.
$b = new foo; // prints foobar just fine.
?>
If you keep your directory/file matching namespace/class consistence the object __autoload works
fine.
But... if you try to give loader.php a namespace you'll obviously get fatal errors.
My sample is just 1 level dir, but I've tested with a very complex and deeper structure. Hope
anybody finds this useful.
Cheers!
----
Server IP: 69.147.83.197
Probable Submitter: 201.1.136.131
----
Manual Page -- http://www.php.net/manual/en/language.namespaces.rules.php
Edit -- https://master.php.net/note/edit/92608
Del: integrated -- https://master.php.net/note/delete/92608/integrated
Del: useless -- https://master.php.net/note/delete/92608/useless
Del: bad code -- https://master.php.net/note/delete/92608/bad+code
Del: spam -- https://master.php.net/note/delete/92608/spam
Del: non-english -- https://master.php.net/note/delete/92608/non-english
Del: in docs -- https://master.php.net/note/delete/92608/in+docs
Del: other reasons-- https://master.php.net/note/delete/92608
Reject -- https://master.php.net/note/reject/92608
Search -- https://master.php.net/manage/user-notes.php