Codeigniter:使用load_class [英] Codeigniter: Use of load_class

查看:235
本文介绍了Codeigniter:使用load_class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自己的记录类,以将数据保存在数据库中。当我看看CI如何做我注意到有一个 log_message()函数处理日志记录。有一个 load_class 函数我不能分配到CI用户指南中的任何内容。



1



希望有一些CI家伙如何回答: - )

解决方案

简短答案:
您可以编写自己的日志类来覆盖默认的CI类:

  <?php 
//这个文件是/application/libraries/MY_Log.php
类MY_Log扩展CI_Log {
public function write_log($ level ='error',$ msg,$ php_error = FALSE)
{
//将自己的日志功能放在这里。
//如果你希望它仍然像往常一样记录到一个文件,使用:
parent :: write_log($ level,$ msg,$ php_error);
}
}

长答案 / p>

load_class()函数基本上是一个单例装载器。如果类已经加载,则返回上一个实例;否则,加载它并创建单例。这在CI的框架中是非常重要的。你必须知道,每次你调用,说,一个数据库函数,它是应用于同一个对象,而不是实例化一个新的(这将是非常混乱)。默认情况下,所有CI库都以此方式运行。



重要提示:它们在2.0版本中更改了此函数的显着性。以前,它只能从 / libraries 文件夹加载,但现在,它将从 / core 加载,



下面是加载过程,比如,Log类(从你的例子):

  $ _ log =& load_class('Log'); 
$ _log-> write_log($ level,$ message,$ php_error);

这会依次执行以下检查:


  1. 如果Log类已经存在,就完成了。

  2. 如果没有,请先检查 / system / libraries 文件夹中的Log.php文件

  3. 如果对于步骤#2没有文件存在,现在为MY_Log.php文件(或任何您的子类前缀)检查 / application / libraries

  4. 如果加载了默认的CI类(来自 / system 文件夹),但您必须 c / c $

  5. 返回类的新实例(YOURS,如果存在);否则返回类的新实例,它是CI_ *类)



我实际上从来不需要使用 load_class code>函数,因为它允许相当无缝地扩展。



因此,要覆盖一个类,首先找到原来的位置(通常 / system / libraries / system / core )。将您的扩展文件放在相应的 / application 文件夹中(这很重要!如果它在 / system / core 扩展必须在 / application / core 下)。使用 MY _ (或您在配置中设置的任何内容)为文件名和类名添加前缀,并将它扩展 CI _ 基类。


I am writing my own logging class to save data in a DB. As I looked how CI is doing I noticed there is a log_message() function which handles the logging. There is a load_class function I can't assign to anything in the CI user guide.

1 Why do they put this into an extra function?

2 What/where loads this function files from?

Hope there are some CI guys how can answer :-)

解决方案

Short answer: You can write your own log class to override the default CI class:

<?php
// this file is /application/libraries/MY_Log.php
class MY_Log extends CI_Log {
    public function write_log($level = 'error', $msg, $php_error = FALSE)
    {
        // Put your own logging function in here.
        // If you want it to still log to a file as usual, use this:
        parent::write_log($level, $msg, $php_error);
    }
}

Long answer:

The load_class() function is basically a singleton loader. If the class has already been loaded, return a previous instance; otherwise, load it and create the singleton. It is very important in a framework like CI. You have to know that every time you call, say, a database function, it is applying it to the same object, not instantiating a new one (that would get really messy). All CI libraries function this way by default.

An important note: they changed how this functions significantly in version 2.0. Previously, it would only load from the /libraries folder, but now, it will load from /core or wherever you specify when calling the function.

Here's the process for loading, say, the Log class (from your example):

$_log =& load_class('Log');
$_log->write_log($level, $message, $php_error);

This runs the following checks, in sequence:

  1. If the Log class already exists, we're done. Return the singleton.
  2. If not, first check the /system/libraries folder for a "Log.php" file
  3. If no file existed for step #2, now check /application/libraries for a "MY_Log.php" file (or whatever your subclass prefix is set to in your configuration)
  4. If it loaded the default CI class (from the /system folder), but you DO have an extended class under /application, load that class too.
  5. Return a new instance of the class (YOURS if it exists; otherwise, it's the CI_* class)

I've actually never needed to use the load_class() function, as it allows extension fairly seamlessly. However, it's good to know how it works.

So, to override a class, first find where the original resides (usually /system/libraries or /system/core). Put your extending file in the corresponding /application folder (this is important! If it's under /system/core, the extension MUST be under /application/core). Prefix both the filename and the class name with MY_ (or whatever you set in your configuration), and have it extend the CI_ base class.

这篇关于Codeigniter:使用load_class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆