If you like to declare an __autoload function within a namespace or class, use the spl_autoload_register() function to register it and it will work fine.
Regras de resolução de nomes
Nomes são resolvidos seguindo estas regras de resolução:
-
Todos nomes qualificados são traduzidos durante a compilação de acordo com a
atual regra do import. Por exemplo, se o namespace A\B\C é importado, uma chamada para
C\D\e()é traduzida paraA\B\C\D\e(). -
Nomes não qualificados de classes são traduzidos durante compilação de acordo com a atual
regra de import (nome completo substituído pelo pequeno nome importado). Por exemplo, se
o namespace A\B\C é importado,
new C()é traduzido paranew A\B\C(). - Dentro do namespace, chamadas para nomes não qualificados de funções que são definidos no atual namespace (e é conhecido na hora que a chamada é analisada) são interpretados como chamadas para estas funções do namespace, em tempo de compilação.
-
Dentro do namespace (digo A\B), chamadas para funções não qualificadas que não são
definidas no atual namespace são resolvidos em tempo de execução. Veja
como uma chamada para uma função foo() é resolvida:
- Ele procura por uma função do atual namespace: A\B\foo().
- Ele procura e tenta chamar a função interna foo().
-
Dentro do namespace (digo A\B), chamadas para não
qualificados nomes de classes são resolvidos em tempo de execução.
Veja como uma chamada para
new C()é resolvida:- Ele verifica por uma classe do namespace atual: A\B\C.
- Ele tenta buscar e chamar a classe interna C.
- Ele tenta fazer autoload A\B\C.
new \C()tem que ser usado. -
Chamadas para qualificadas funções são resolvidas em tempo de execução.
Veja como a chamada para A\B\foo() é resolvida:
- Ele verifica por uma função foo() no namespace A\B.
- Ele verifica por uma classe A\B e chama o método estático foo(). Irá fazer autoload da class se necessário.
-
Qualificados nomes de classes são resolvidos em tempo de compilação como classes
correspondentes do namespace. Por exemplo,
new A\B\C()refere-se a classe C do namespace A\B.
Exemplo #1 Ilustrando resolução de nomes
<?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
?>
kdimi ¶
2 years ago
safakozpinar at NOSPAM dot gmail dot com ¶
2 years ago
As working with namespaces and using (custom or basic) autoload structure; magic function __autoload must be defined in global scope, not in a namespace, also not in another function or method.
<?php
namespace Glue {
/**
* Define your custom structure and algorithms
* for autoloading in this class.
*/
class Import
{
public static function load ($classname)
{
echo 'Autoloading class '.$classname."\n";
require_once $classname.'.php';
}
}
}
/**
* Define function __autoload in global namespace.
*/
namespace {
function __autoload ($classname)
{
\Glue\Import::load($classname);
}
}
?>
rangel ¶
3 years ago
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!
rangel ¶
3 years ago
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!
sammaye ¶
3 years ago
I have noticed one problem with __autoload function. Say you have two namespaces, one is a sub of the other:
\Glue
\Glue\Import
Within that Import namespace you have a function auto() with the magic __autoload inside. No matter what you do that auto() will never traverse it's sub function meaning you will nevber get an __autoload function.
Even if you put the __autoload within a class within the namespace as such:
<?php
namespace Glue\Import;
class import{
private static $_AutoLoad = array();
private static $_Imported = array();
function load($sName){
if(! isset(self::$_AutoLoad[$sName]))
//throw new ImportError("Cannot import module with name '$sName'.");
echo("file with name '$sName' failed to load with path '".self::$_AutoLoad[$sName]."'");
if(! isset(self::$_Imported[$sName])){
self::$_Imported[$sName] = True;
return include_once(self::$_AutoLoad[$sName]);
}
}
function push($sName, $sPath){
self::$_AutoLoad[$sName] = $sPath;
}
function auto(){
function __autoload($sClass){
load($sClass);
}
}
}
?>
It will not work. Just something to keep in mind.
