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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀