我正在尝试在命名空间中使用特征。
namespace {
class Glob {
function __toString() {
return 'Global Namespace<br />';
}
}
$Ng = new \special\Glob();
echo $Ng;
}
namespace special {
trait myTrait {
public $config = 'This is Config Trait';
}
class Glob {
use \special\myTrait;
public $v = [];
function __toString() {
return 'Special Namespace<br />';
}
function __set( $index,$value ){
$this->v[$index] = $value;
}
function __get( $index ){
$this->v[$index];
}
}
}
请注意,这只是一个测试,因此这里的自动加载并不重要。
它引发致命错误:
Fatal error: Class 'special\Glob' not found in C:\wamp\www\Learnings\namespace.php on line 23
但是在全局命名空间中它很有用。
答案 0 :(得分:2)
交换名称空间块,例如:
namespace special {
trait myTrait {
public $config = 'This is Config Trait';
}
class Glob {
use \special\myTrait;
public $v = [];
function __toString() {
return 'Special Namespace<br />';
}
function __set( $index,$value ){
$this->v[$index] = $value;
}
function __get( $index ){
$this->v[$index];
}
}
}
namespace {
class Glob {
function __toString() {
return 'Global Namespace<br />';
}
}
$Ng = new \special\Glob();
echo $Ng;
}