dougu2006 2013-06-15 19:08
浏览 29

如果我尝试从其他函数内部创建类的实例,则自动加载不起作用

helper.php : Required at the top of index.php

function __autoload($className) {

    $path = ROOT_DIR.'/class/'.strtolower(trim($className)).'.class.php';

    if (file_exists($path)) {
        require_once($path);
    }
}

function load_libs($class_variable_name) {

    global $session;
    global $_libs;

    $_libs[$class_variable_name] = new $class_variable_name($session);

    return $_libs[$class_variable_name];
}

From index.php I create an instance of a class :

new some_class_one();

No problem, it loads class file via __autoload.

But, in case that I call :

load_libs('some_class_two');

I getting error : Fatal error: Class 'some_class_two' not found in C:\wamp\www\helper.php on line

Any idea what to do ?

  • 写回答

1条回答 默认 最新

  • dp518158 2013-12-20 15:27
    关注

    I just tested this on my machine and it works perfectly. I could not replicate the issue. This is the version of PHP I'm running:

    $ php -v
    PHP 5.4.17 (cli) (built: Aug 25 2013 02:03:38) 
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    

    I would suggest that you remove the .class from your filenames. It's better to use namespaces and have the namespace/class name match the filesystem. Other than that, either your ROOT_DIR is incorrect or your file is located in the wrong place.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么