dtz88967 2013-06-13 12:10
浏览 49
已采纳

带有命名空间类的__autoload会为找不到的文件提供致命错误

I was going through the __autoload feature that PHP provides.

However this is the index.php I have:-

define('app_path', realpath('../'));

$paths = array(
        app_path, get_include_path());
set_include_path(implode(PATH_SEPARATOR, $paths));

function __autoload($classname)
{
    $filename = str_replace('\\', '/', $classname.'.php');
    require_once $filename;
}

use \engine\controllers as Controllers;

$data = new Controllers\base(); // This one is line no. 25 (Here is error)
echo $data->mind('Hi');

And this one as my base.php:-

 namespace controllers;
 class base {
    public function __construct() {
        echo __CLASS__ . '<br/>';
        echo __NAMESPACE__;
    }

    public function mind($myname)
    {
        echo $myname;
    }
 }

and throws this error:

enter image description here

My directory structure is as follows:

app -> engine -> controller -> base.php

app -> index.php

I am not sure whats wrong is happening. I am just learning how to use namespace and __autoload

I also tried spl_autoload_register but did not success. Kindly suggest.

EDIT:1

Also, if i want to replace it with spl_autoload_register how that can be implemented.

  • 写回答

1条回答 默认 最新

  • dongpu42006096 2013-06-13 12:18
    关注

    Not sure, but worth a try:

    In base.php, change to namespace engine\controllers; on line 1.

    And in index.php, change to use engine\controllers as Controllers; (remove leading backslash) on line 23.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站