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
?>
Namespaces und dynamische Sprachfeatures
(PHP 5 >= 5.3.0)
Die Implementierung von Namespaces in PHP ist stark von seinen Eigenschaften als Programmiersprache mit dynamischen Features beeinflusst. Man kann also den folgenden Code in Code mit Namespaces umformen:
Beispiel #1 Dynamischer Zugriff auf Elemente
example1.php:
<?php
class classname
{
function __construct()
{
echo __METHOD__,"\n";
}
}
function funcname()
{
echo __FUNCTION__,"\n";
}
const constname = "global";
$a = 'classname';
$obj = new $a; // gibt classname::__construct aus
$b = 'funcname';
$b(); // gibt funcname aus
echo constant('constname'), "\n"; // gibt global aus
?>
Beispiel #2 Dynamischer Zugriff auf Elemente mit Namespace
<?php
namespace namespacename;
class classname
{
function __construct()
{
echo __METHOD__,"\n";
}
}
function funcname()
{
echo __FUNCTION__,"\n";
}
const constname = "namespaced";
include 'example1.php';
$a = 'classname';
$obj = new $a; // gibt classname::__construct aus
$b = 'funcname';
$b(); // gibt funcname aus
echo constant('constname'), "\n"; // gibt global aus
/* Wenn man doppelte Anführungszeichen verwendet,
muss "\\namespacename\\classname" verwendet werden */
$a = '\namespacename\classname';
$obj = new $a; // gibt namespacename\classname::__construct aus
$a = 'namespacename\classname';
$obj = new $a; // gibt ebenfalls namespacename\classname::__construct aus
$b = 'namespacename\funcname';
$b(); // gibt namespacename\funcname aus
$b = '\namespacename\funcname';
$b(); // gibt ebenfalls namespacename\funcname aus
echo constant('\namespacename\constname'), "\n"; // gibt namespaced aus
echo constant('namespacename\constname'), "\n"; // gibt ebenfalls namespaced aus
?>
Bitte lesen Sie auch den Hinweis zum Escaping von Namespacenamen in Strings.
scott at intothewild dot ca ¶
3 years ago
guilhermeblanco at php dot net ¶
3 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
?>
Alexander Kirk ¶
1 year 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)
