好的!!!现在,我不知道为什么spl_autoload_register((不能加载类了。
我的文件夹结构是这样的。。
-
应用
- 控制器
- welcome.php
- 控制器
-
系统
- 堆芯
- BaseController.php
- 加载.php
- 堆芯
-
index.php
My BaseController.php代码
<?php
namespace system'core;
class BaseController {
public function __construct() {
spl_autoload_register(array($this, 'loader'));
}
private function loader($className) {
$className = ltrim($className, '''');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '''')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('''', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
}
My Load.php代码
<?php
namespace system'core;
class Load {
public function view()
{
echo "Method for loading view";
}
}
我的welcome.php代码
<?php
class Welcome extends system'core'BaseController {
public function index()
{
$obj_load = new Load();
$obj_load->view();
}
}
我的index.php代码
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once "system/core/BaseController.php";
require_once "application/controller/welcome.php";
$welcome = new welcome();
echo $welcome->index();
当我执行这个代码(index.php(时,我会得到以下错误。。。
Fatal error: Class 'Load' not found in /var/www/nut/test/application/controller/welcome.php on line 5
但是,如果我从Load.php中删除名称空间,我不会得到任何错误。我不明白为什么这个名称空间(在Load.php中使用(会产生错误。
任何想法。。。
问候
正如Mark所指出的,在试图包含文件时使用了错误的字符。这一页应该会让事情变得更清楚。
编辑:你的代码中有不少错误。名称空间的全部意义在于,你必须正确地引用它们(以及正确地声明它们,两者都没能做到,这就是自动加载代码不起作用的原因(。此外,自动加载代码没有真正的运行机会,因为你把它隐藏在一个构造函数中,而这个构造函数一开始就不会加载。试试这个:
index.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once("system/core/AutoLoader.php");
$welcome = new 'application'controller'welcome();
echo $welcome->index();
welcome.php(注意小写类名和名称空间的添加(
<?php
namespace application'controller;
class welcome extends 'system'core'BaseController {
public function index()
{
$obj_load = new 'system'core'Load();
return $obj_load->view();
}
}
加载.php
<?php
namespace system'core;
class Load {
public function view()
{
return "Method for loading view";
}
}
BaseController.php
<?php
namespace system'core;
class BaseController {
// public function __construct() {
// spl_autoload_register(array($this, 'loader'));
// }
//
// private function loader($className) {
// $className = ltrim($className, '''');
// $fileName = '';
// $namespace = '';
// if ($lastNsPos = strrpos($className, '''')) {
// $namespace = substr($className, 0, $lastNsPos);
// $className = substr($className, $lastNsPos + 1);
// $fileName = str_replace('''', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
// }
// $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
//
// require $fileName;
// }
}
新文件:system''core''AutoLoader.php
<?php
namespace system'core;
class AutoLoader {
static public function loader($className) {
$className = ltrim($className, '''');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '''')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('''', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
}
spl_autoload_register(array(''system'core'AutoLoader', 'loader'));
我还更改了代码以返回字符串,而不是显示它,因为您已经在index.php 中回显了它