【问题标题】:What's wrong in my autoload code我的自动加载代码有什么问题
【发布时间】:2015-10-30 00:11:19
【问题描述】:

我确实测试了两个代码以将类自动加载到页面,但对我不起作用!

怎么了?

spl_autoload_extensions('.php, .class.php');
    spl_autoload_register(function ($name){
        if('inc/'."{$name}".'.class.php'){
            require('inc/'."{$name}".'.class.php');
        }
    });

与:

class FW{
        public static function autoload($class){
            $class = strtolower($class);
            $fpath = realpath(dirname(__FILE__)).'/inc/'."{$class}". '.class.php';
            if(is_readable($fpath)){
                require_once($fpath);
            }
        }
    }

    spl_autoload_register('FW::autoload');

【问题讨论】:

  • 您是否遇到任何错误?
  • 是的,致命错误:找不到类“数据库”..
  • 您应该在问题中包含错误,而不是在 cmets 中。

标签: php spl-autoload-register


【解决方案1】:

由于我的问题没有回答,所以我回复了答案,希望它会有用。 我将代码添加到 Config.php 并将类名称样式从whatever.class.php 编辑为class.whatever.php,这对我有用。

    //  Classes Autoloader
    spl_autoload_extensions('.php, .class.php');
    spl_autoload_register(function ($name){
        if(is_readable(realpath(dirname(__FILE__)).'/inc/class.'."{$name}".'.php')){
            require(realpath(dirname(__FILE__)).'/inc/class.'."{$name}".'.php');
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2019-01-03
    • 2011-07-28
    相关资源
    最近更新 更多