【发布时间】:2014-01-06 23:58:18
【问题描述】:
我在使用特征和命名空间时遇到错误,因为找不到特征。
index.php:
require_once 'src/App.php';
use App\main;
$App = new App();
src/App.php
namespace App\main;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'DataBase.php';
/**
* code
*/
src/DataBase.php
namespace App\DataBase;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Singleton.php';
class DataBase {
use Singleton; // or use App\Singleton
/**
* code
*/
}
src/Singleton.php
namespace App\Singleton.php;
trait Singleton {
/**
* code
*/
}
但是,当我从 Index.php 运行它时,我收到了这个错误:
Fatal error: Trait 'App\DataBase\Singleton' not found in (...)
我该如何解决?
编辑
php在命名空间中自动设置类名,例如:
Bar.php
namespace App;
class Bar {
/**
* code
*/
}
在调用这个包的时候,可以使用App\Bar,也就是说类是默认设置的。
【问题讨论】:
-
use App\maininindex.php什么都不做。App\main是命名空间,而不是符号名称
标签: php namespaces traits