如何销毁Codeigniter库实例 [英] How to destroy Codeigniter library instance

查看:73
本文介绍了如何销毁Codeigniter库实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有什么方法可以销毁Codeigniter库实例。

I would like to know if there is any way I can destroy a Codeigniter library instance.

我想做的事情是这样的:

What I want to do is something like this:

$this->load->library('my_library');
/**
    Code goes here
**/
$this->my_library->destoy_instance();

执行此操作的原因是因为在执行大型脚本时需要释放RAM内存。

The reason I need to do this is because I need to liberate RAM memory while executing a large scripts.

任何帮助将不胜感激。

推荐答案

您可以做到只需使用取消设置或设置 null

You can do simply like that by using unset or setting null.

unset($this->my_library);

OR

$this->my_library = null;

答案也值得您阅读,以了解有关这两种方法的详细信息。

This answer is also worth reading for you to know detail about these two ways.

编辑

没有内置方法可以销毁加载的库对象。但是您可以通过扩展 Loader 类来实现。然后从该类加载和卸载库。这是我的示例代码..

There is no built-in method to destroy loaded library object. But you can do it by extending Loader class. And then load and unload library from that class. Here is my sample code ..

application / libraries / custom_loader.php

application/libraries/custom_loader.php

class Custom_loader extends CI_Loader {
    public function __construct() {
        parent::__construct();
    }

    public function unload_library($name) {
        if (count($this->_ci_classes)) {
            foreach ($this->_ci_classes as $key => $value) {
                if ($key == $name) {
                    unset($this->_ci_classes[$key]);
                }
            }
        }

        if (count($this->_ci_loaded_files)) {
            foreach ($this->_ci_loaded_files as $key => $value)
            {
                $segments = explode("/", $value);
                if (strtolower($segments[sizeof($segments) - 1]) == $name.".php") {
                    unset($this->_ci_loaded_files[$key]);
                }
            }
        }

        $CI =& get_instance();
        $name = ($name != "user_agent") ? $name : "agent";
        unset($CI->$name);
    }
}

在您的控制器中。.

$this->load->library('custom_loader');
// To load library
$this->custom_loader->library('user_agent');
$this->custom_loader->library('email');

// To unload library
$this->custom_loader->unload_library('user_agent');
$this->custom_loader->unload_library('email');

希望它会有用。

这篇关于如何销毁Codeigniter库实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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