Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter:在核心类中加载库_Php_Codeigniter - Fatal编程技术网

Php Codeigniter:在核心类中加载库

Php Codeigniter:在核心类中加载库,php,codeigniter,Php,Codeigniter,我想用几个函数扩展/system/core/Log.php库。如果出现错误,一个函数应该通过自定义函数sendMail()发送邮件,该函数是custom_library.php的一部分 因此,我创建了文件/application/core/MY_Log.php class MY_Log extends CI_Log { public function __construct() { parent::__construct(); } public function wri

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

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

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;
  }
}
问题:我无法加载
自定义库.php
。这些方法都不起作用:

//approach 1    
$this->load->library('Custom_library');

//approach 2  
$CI =& get_instance();
$CI->load->library('Custom_library');
进近2的错误信息:

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\u class
尝试了这种方法

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_”的类,因此抛出错误消息:

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

如果需要访问其他库,则扩展
CI_Log
将不起作用。原因是
CI\u Log
是在创建
$CI
之前很久创建的,因此没有可供
&get\u instance()
返回的“实例”

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

解决这个问题的方法可能不止一种。在我看来,黑客攻击最少的方法是让您的logger类利用
CI\u Log
而不是
extend

application/libraries/Logger.php

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);
    }

}
您的“记录器”需要加载到与任何其他库相同的控制器中

$this->load->library('logger');
使用示例可能如下所示

$this->logger->write_log('error', "This is FUBAR");
当您调用
$this->load->library('logger')
已创建
日志
类,它是
$CI
的一部分(也称
$this
)。所以这条线

    //use the built-in logging mechanism, a.k.a. CI_Log
    return log_message($level, $msg);
可以这样做

    //use the built-in logging mechanism, a.k.a. CI_Log
    return $this->CI->log->write_log($level, $msg);
由于
log\u消息
所做的一切都是调用
log->write\u log
,因此这样做的效率会稍微高一些。我认为这样做而不是使用
log\u message
没有任何问题


这是一个有趣的问题,通过研究我学到了很多。谢谢。

谢谢您的意见。我更新了我的问题-不确定我是否以正确的方式应用了你的建议。请参阅我修改了很多的答案。感谢您对codeigniter的重要见解,这使我找到了解决方法。我将在几天后发布它。仔细查看“方法2”的错误消息,我会问;
Custom\u库
是控制器,即
class Custom\u库扩展了CI\u控制器
Custom\u库
是存储在
应用程序/库
中的库,不扩展
CI\u控制器
而是->
class Custom\u库{…