PHP 5.6.0RC3 is available

Espaces de noms et langage dynamique

(PHP 5 >= 5.3.0)

L'implémentation des espaces de noms de PHP est influencée par sa nature dynamique de langage de programmation. Par conséquent, pour convertir du code tel que le code de l'exemple suivant, en un espace de noms :

Exemple #1 Accès dynamique aux éléments

example1.php:

<?php
class classname
{
    function 
__construct()
    {
        echo 
__METHOD__,"\n";
    }
}
function 
funcname()
{
    echo 
__FUNCTION__,"\n";
}
const 
constname "global";

$a 'classname';
$obj = new $a// affiche classname::__construct
$b 'funcname';
$b(); // affiche funcname
echo constant('constname'), "\n"// affiche global
?>
Il faut utiliser un nom absolu (le nom de la classe, avec son préfixe d'espace de noms). Notez qu'il n'y a pas de différence entre un nom absolu et un nom qualifié dans un nom de classe, de fonction ou de constante dynamique, ce qui fait que l'anti-slash initial n'est pas nécessaire.

Exemple #2 Accès dynamique à des espaces de noms

<?php
namespace nomdelespacedenoms;
class 
classname
{
    function 
__construct()
    {
        echo 
__METHOD__,"\n";
    }
}
function 
funcname()
{
    echo 
__FUNCTION__,"\n";
}
const 
constname "namespaced";

include 
'example1.php';

$a 'classname';
$obj = new $a// affiche classname::__construct
$b 'funcname';
$b(); // affiche funcname
echo constant('constname'), "\n"// affiche global

/* Note que si vous utilisez des guillemets doubles, "\\nomdelespacedenoms\\classname" doit être utilisé */
$a '\nomdelespacedenoms\classname';
$obj = new $a// affiche nomdelespacedenoms\classname::__construct
$a 'nomdelespacedenoms\classname';
$obj = new $a// affiche aussi nomdelespacedenoms\classname::__construct
$b 'nomdelespacedenoms\funcname';
$b(); // affiche nomdelespacedenoms\funcname
$b '\nomdelespacedenoms\funcname';
$b(); // affiche aussi nomdelespacedenoms\funcname
echo constant('\nomdelespacedenoms\constname'), "\n"// affiche namespaced
echo constant('nomdelespacedenoms\constname'), "\n"// affiche aussi namespaced
?>

Il est recommandé de lire la note au sujet de la protection des espaces de noms dans les chaînes.

add a note add a note

User Contributed Notes 4 notes

up
15
Alexander Kirk
3 years ago
When extending a class from another namespace that should instantiate a class from within the current namespace, you need to pass on the namespace.

<?php // File1.php
namespace foo;
class
A {
    public function
factory() {
        return new
C;
    }
}
class
C {
    public function
tell() {
        echo
"foo";
    }
}
?>

<?php // File2.php
namespace bar;
class
B extends \foo\A {}
class
C {
    public function
tell() {
        echo
"bar";
    }
}
?>

<?php
include "File1.php";
include
"File2.php";
$b = new bar\B;
$c = $b->factory();
$c->tell(); // "foo" but you want "bar"
?>

You need to do it like this:

When extending a class from another namespace that should instantiate a class from within the current namespace, you need to pass on the namespace.

<?php // File1.php
namespace foo;
class
A {
    protected
$namespace = __NAMESPACE__;
    public function
factory() {
       
$c = $this->namespace . '\C';
        return new
$c;
    }
}
class
C {
    public function
tell() {
        echo
"foo";
    }
}
?>

<?php // File2.php
namespace bar;
class
B extends \foo\A {
    protected
$namespace = __NAMESPACE__;
}
class
C {
    public function
tell() {
        echo
"bar";
    }
}
?>

<?php
include "File1.php";
include
"File2.php";
$b = new bar\B;
$c = $b->factory();
$c->tell(); // "bar"
?>

(it seems that the namespace-backslashes are stripped from the source code in the preview, maybe it works in the main view. If not: fooA was written as \foo\A and barB as bar\B)
up
2
akhoondi+php at gmail dot com
11 months ago
It might make it more clear if said this way:

One must note that when using a dynamic class name, function name or constant name, the "current namespace", as in http://www.php.net/manual/en/language.namespaces.basics.php is global namespace.

One situation that dynamic class names are used is in 'factory' pattern. Thus, add the desired namespace of your target class before the variable name.

namespaced.php
<?php
// namespaced.php
namespace Mypackage;
class
Foo {
    public function
factory($name, $global = FALSE)
    {
        if (
$global)
           
$class = $name;
        else
           
$class = 'Mypackage\\' . $name;
        return new
$class;
    }
}

class
A {
    function
__construct()
    {
        echo
__METHOD__ . "<br />\n";
    }
}
class
B {
    function
__construct()
    {
        echo
__METHOD__ . "<br />\n";
    }
}
?>

global.php
<?php
// global.php
class A {
    function
__construct()
    {
        echo 
__METHOD__;
    }
}
?>

index.php
<?php
//  index.php
namespace Mypackage;
include(
'namespaced.php');
include(
'global.php');
 
 
$foo = new Foo();
 
 
$a = $foo->factory('A');        // Mypackage\A::__construct
 
$b = $foo->factory('B');        // Mypackage\B::__construct
 
 
$a2 = $foo->factory('A',TRUE);    // A::__construct
 
$b2 = $foo->factory('B',TRUE);    // Will produce : Fatal error: Class 'B' not found in ...namespaced.php on line ...
?>
up
3
guilhermeblanco at php dot net
5 years ago
Please be aware of FQCN (Full Qualified Class Name) point.
Many people will have troubles with this:

<?php

// File1.php
namespace foo;

class
Bar { ... }

function
factory($class) {
    return new
$class;
}

// File2.php
$bar = \foo\factory('Bar'); // Will try to instantiate \Bar, not \foo\Bar

?>

To fix that, and also incorporate a 2 step namespace resolution, you can check for \ as first char of $class, and if not present, build manually the FQCN:

<?php

// File1.php
namespace foo;

function
factory($class) {
    if (
$class[0] != '\\') {
        echo
'->';
        
$class = '\\' . __NAMESPACE__ . '\\' . $class;
    }

    return new
$class();
}

// File2.php
$bar = \foo\factory('Bar'); // Will correctly instantiate \foo\Bar

$bar2 = \foo\factory('\anotherfoo\Bar'); // Wil correctly instantiate \anotherfoo\Bar

?>
up
1
scott at intothewild dot ca
4 years ago
as noted by guilhermeblanco at php dot net,

<?php

 
// fact.php

 
namespace foo;

  class
fact {

    public function
create($class) {
      return new
$class();
    }
  }

?>

<?php

 
// bar.php

 
namespace foo;

  class
bar {
  ...
  }

?>

<?php

 
// index.php

 
namespace foo;

  include(
'fact.php');
 
 
$foofact = new fact();
 
$bar = $foofact->create('bar'); // attempts to create \bar
                                  // even though foofact and
                                  // bar reside in \foo

?>
To Top