downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Exceções> <__NAMESPACE__
[edit] Last updated: Fri, 26 Apr 2013

view this page in

Regras de resolução de nomes

Nomes são resolvidos seguindo estas regras de resolução:

  1. 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 para A\B\C\D\e().
  2. 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 para new A\B\C().
  3. 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.
  4. 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:
    1. Ele procura por uma função do atual namespace: A\B\foo().
    2. Ele procura e tenta chamar a função interna foo().
    Para chamar a função definida do usuário no namespace global, \foo() tem que ser usado.
  5. 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:
    1. Ele verifica por uma classe do namespace atual: A\B\C.
    2. Ele tenta buscar e chamar a classe interna C.
    3. Ele tenta fazer autoload A\B\C.
    Para referenciar a classe definida pelo usuário no namespace global, new \C() tem que ser usado.
  6. Chamadas para qualificadas funções são resolvidas em tempo de execução. Veja como a chamada para A\B\foo() é resolvida:
    1. Ele verifica por uma função foo() no namespace A\B.
    2. Ele verifica por uma classe A\B e chama o método estático foo(). Irá fazer autoload da class se necessário.
  7. 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
?>


Exceções> <__NAMESPACE__
[edit] Last updated: Fri, 26 Apr 2013
 
add a note add a note User Contributed Notes Regras de resolução de nomes - [5 notes]
up
1
kdimi
2 years ago
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.
up
3
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!
up
0
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!
up
0
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);
    }

}
?>
up
0
sammaye
2 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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites