【发布时间】:2014-09-30 06:51:42
【问题描述】:
在index.php我有以下代码
require 'Bootstrap.php';
require 'Variables.php';
function __autoload($class){
$class = str_replace('Control\\', '', $class);
require_once 'class/'.$class.'.php';
}
$bootstratp = new Control\Bootstrap();
在Bootstrap.php
namespace Control;
class Bootstrap{
function __construct(){
Constructor::load_html();
self::same_namespace_different_class();
}
static function same_namespace_different_class(){
Variables::get_values();
}
}
在class/Constructor.php
class Constructor{
static function load_html(){
echo 'html_loaded';
}
static function load_variables(){
echo 'load variables';
}
}
在Variables.php
namespace Control;
class Variables{
static function get_values(){
Constructor::load_variables();
}
}
假设,我总共有 4 个 PHP 文件,包括 2 个不同命名空间的 3 个 Class 文件。我在index.php 上还有一个__autoload 函数,它将调用“类”文件夹中的类,但我的“控制”命名空间文件位于根文件夹中。
当我在 __autoload 中回显类名时,即使我从全局命名空间调用一个类,我也会得到所有以“Control\”开头的类名。
我遇到了错误
Warning: require_once(class/Variables.php): failed to open stream: No such file or directory in _____________ on line 10
我的代码有什么问题??
【问题讨论】:
标签: php class namespaces autoload bootstrapping