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.

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

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应