首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >代码点火器:核心类中的加载库

代码点火器:核心类中的加载库
EN

Stack Overflow用户
提问于 2016-10-31 21:28:47
回答 1查看 4.5K关注 0票数 1

我想用几个函数扩展/system/core/Log.php库。在发生错误时,一个函数应该通过自定义函数sendMail()发送邮件,该函数是Custom_library.php的一部分。

因此,我创建了文件/application/core/MY_Log.php

代码语言:javascript
运行
复制
class MY_Log extends CI_Log {
  public function __construct()
  {
    parent::__construct();
  }

  public function write_log($level, $msg)
  {
    $result = parent::write_log($level, $msg);

    return $result;
  }
}

问题:,我无法加载Custom_library.php。这些办法都没有奏效:

代码语言:javascript
运行
复制
//approach 1    
$this->load->library('Custom_library');

//approach 2  
$CI =& get_instance();
$CI->load->library('Custom_library');

方法2的错误消息:

代码语言:javascript
运行
复制
Fatal error: Uncaught Error: Class 'CI_Controller' not found in /home/gp/public_html/system/core/CodeIgniter.php:366 Stack trace: #0 /home/gp/public_html/application/core/MY_Log.php(13): get_instance() #1 /home/gp/public_html/system/core/Common.php(478): MY_Log->write_log('error', 'Severity: error...') #2 /home/gp/public_html/system/core/Exceptions.php(105): log_message('error', 'Severity: error...') #3 /home/gp/public_html/system/core/Common.php(662): CI_Exceptions->log_exception('error', 'Exception: Clas...', '/home/gp/public...', 366) #4 [internal function]: _exception_handler(Object(Error)) #5 {main} thrown in /home/gp/public_html/system/core/CodeIgniter.php on line 366

的问题:可以在核心类中加载和使用库吗?

更新

我通过函数&load_class尝试了这种方法。

代码语言:javascript
运行
复制
function &load_class($class, $directory = 'libraries', $param = NULL)
    {
        static $_classes = array();

        // Does the class exist? If so, we're done...
        if (isset($_classes[$class]))
        {
            return $_classes[$class];
        }

        $name = FALSE;

        // Look for the class first in the local application/libraries folder
        // then in the native system/libraries folder
        foreach (array(APPPATH, BASEPATH) as $path)
        {
            if (file_exists($path.$directory.'/'.$class.'.php'))
            {
                $name = 'CI_'.$class;

                if (class_exists($name, FALSE) === FALSE)
                {
                    require_once($path.$directory.'/'.$class.'.php');
                }

                break;
            }
        }

        // Is the request a class extension? If so we load it too
        if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
        {
            $name = config_item('subclass_prefix').$class;

            if (class_exists($name, FALSE) === FALSE)
            {
                require_once(APPPATH.$directory.'/'.$name.'.php');
            }
        }

        // Did we find the class?
        if ($name === FALSE)
        {
            // Note: We use exit() rather than show_error() in order to avoid a
            // self-referencing loop with the Exceptions class
            set_status_header(503);
            echo 'Unable to locate the specified class: '.$class.'.php';
            exit(5); // EXIT_UNK_CLASS
        }

        // Keep track of what we just loaded
        is_loaded($class);

        $_classes[$class] = isset($param)
            ? new $name($param)
            : new $name();
        return $_classes[$class];
    }

如果我运行函数$CI =& load_class('Custom_library');,它会找到正确的文件,但查找前缀为"CI_“的类,从而引发和错误消息:

代码语言:javascript
运行
复制
Fatal error: Uncaught Error: Class 'CI_Custom_library' not found in /home/gp/public_html/system/core/Common.php:196 Stack trace: #0 /home/gp/public_html/application/core/MY_Log.php(14): load_class('Custom_library') #1 /home/gp/public_html/system/core/Common.php(478): MY_Log->write_log('error', 'Severity: error...') #2 /home/gp/public_html/system/core/Exceptions.php(105): log_message('error', 'Severity: error...') #3 /home/gp/public_html/system/core/Common.php(662): CI_Exceptions->log_exception('error', 'Exception: Clas...', '/home/gp/public...', 196) #4 [internal function]: _exception_handler(Object(Error)) #5 {main} thrown in /home/gp/public_html/system/core/Common.php on line 196
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-31 23:33:05

如果需要访问其他库,扩展CI_Log将无法工作。原因是在创建CI_Log之前很久就创建了$CI,因此&get_instance()无法返回“实例”。

$this->load无法工作,因为$this不是控制器($this$CI指向同一个对象),类load ('CI_Loader')也尚未创建。

可能有不止一种方法可以绕过这件事。在我看来,最不受黑客攻击的方法是让您的记录器类使用CI_Log而不是extend

application/libraries/Logger.php

代码语言:javascript
运行
复制
class Logger
{
    protected $CI;

    public function __construct()
    {
        $this->CI = & get_instance();
        $this->CI->load->library('custom_library');
    }

    public function write_log($level, $msg)
    {
        //do stuff with "custom_library"
         $this->CI->custom_library->some_function();

        //use the built-in logging mechanism, a.k.a. CI_Log
        return log_message($level, $msg);
    }

}

您的“记录器”需要与任何其他库一样加载到Controller中。

代码语言:javascript
运行
复制
$this->load->library('logger');

一个用法示例可能如下所示

代码语言:javascript
运行
复制
$this->logger->write_log('error', "This is FUBAR");

当您调用$this->load->library('logger');时,log类已经创建,并且是$CI (a.k.a )的一部分。$this)。所以这条线

代码语言:javascript
运行
复制
    //use the built-in logging mechanism, a.k.a. CI_Log
    return log_message($level, $msg);

可以用这种方式来代替

代码语言:javascript
运行
复制
    //use the built-in logging mechanism, a.k.a. CI_Log
    return $this->CI->log->write_log($level, $msg);

这会稍微提高一些效率,因为log_message所做的一切都是调用log->write_log。我不认为这样做有什么问题,而不用使用log_message

有趣的问题,我从调查中学到了很多。谢谢。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40350883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档