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 nsfoo // ns\foo is loaded here
{
public function __construct()
{
parent::__construct();
echo "<br />foo" . $this->say;
}
}
$a = new nsfoo(); // 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!
Name resolution rules
Names are resolved following these resolution rules:
-
All qualified names are translated during compilation according to current
import rules. In example, if the namespace A::B::C is imported, a call to
C::D::e()is translated toA::B::C::D::e(). -
Unqualified class names are translated during compilation according to current
import rules (full name substituted for short imported name). In example, if
the namespace A::B::C is imported,
new C()is translated tonew A::B::C(). - Inside namespace, calls to unqualified functions that are defined in the current namespace (and are known at the time the call is parsed) are interpreted as calls to these namespace functions, at compile time.
-
Inside namespace (say A::B), calls to unqualified functions that are not
defined in current namespace are resolved at run-time. Here is how a
call to function foo() is resolved:
- It looks for a function from the current namespace : A::B::foo().
- It tries to find and call the internal function foo().
-
Inside namespace (say A::B), calls to unqualified class names are
resolved at run-time. Here is how a call to
new C()is resolved:- It looks for a class from the current namespace : A::B::C().
- It tries to find and call the internal class C().
- It attemts to autoload A::B::C(). C().
new ::C()has to be used. -
Calls to qualified functions are resolved at run-time. Here is how a call
to A::B::foo() is resolved:
- It looks for a function foo() in the namespace A::B.
- It looks for a class A::B and call its static method foo(). It will autoload the class if necessary.
-
Qualified class names are resolved in compile-time as class from corresponding
namespace. For example,
new A::B::C()refers to class C from namespace >A::B.
Example#1 Name resolutions illustrated
<?php
namespace A;
// function calls
foo(); // first tries to call "foo" defined in namespace "A"
// then calls internal function "foo"
::foo(); // calls function "foo" defined in global scope
// class references
new B(); // first tries to create object of class "B" defined in namespace "A"
// then creates object of internal class "B"
new ::B(); // creates object of class "B" defined in global scope
// static methods/namespace functions from another namespace
B::foo(); // first tries to call function "foo" from namespace "A::B"
// then calls method "foo" of internal class "B"
::B::foo(); // first tries to call function "foo" from namespace "B"
// then calls method "foo" of class "B" from global scope
// static methods/namespace functions of current namespace
A::foo(); // first tries to call function "foo" from namespace "A::A"
// then tries to call method "foo" of class "A" from namespace "A"
// then tries to call function "foo" from namespace "A"
// then calls method "foo" of internal class "A"
::A::foo(); // first tries to call function "foo" from namespace "A"
// then calls method "foo" of class "A" from global scope
?>
Name resolution rules
rangel
31-Jul-2009 03:48
31-Jul-2009 03:48
rangel
31-Jul-2009 03:47
31-Jul-2009 03:47
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 nsfoo // ns\foo is loaded here
{
public function __construct()
{
parent::__construct();
echo "<br />foo" . $this->say;
}
}
$a = new nsfoo(); // 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!
