Welcome Guest, Not a member yet? Register   Sign In
How to destruct libraries/models from CI base class?
#1

(This post was last modified: 03-09-2017, 08:01 PM by Zelf.)

How can I destruct a library/model that has been loaded by the CI base class, from the CI base class? I want to keep the CI super object alive, but destroy a library/model that has been loaded. This is for memory reasons on a worker/consumer that runs forever.

PHP Code:
public function worker() {
    while (
true) { // infinite loop. The worker never dies.
        
$message $this->getMessage();

        if (!empty(
$messages)) {
            
$this->load->library('some_library');
            
$this->load->model('some_model');

            
// do something

            // destruct model and library and remove from CI super object, but keep CI super object alive.
        
}
    }

Reply
#2

(This post was last modified: 03-09-2017, 09:53 PM by Zelf.)

This is what I came up with, if any CI developers are free to comment, please let me know why this isn't working. The issue is on a new loop after the library and model are destroyed, they do not load again. I'll get a "Call to member function on null" error.

PHP Code:
class Unloader extends CI_Loader {
    public function 
__construct() {
        
parent::__construct();
    }

    public function 
ci($objects = []) {
        
$CI =& get_instance();

        foreach (
$objects as $object) {
            if (isset(
$CI->load->_ci_classes[$object])) {
                unset(
$CI->load->_ci_classes[$object]);
            }

            if (isset(
$CI->load->_ci_models[array_search($object$CI->load->_ci_modelstrue)])) {
                unset(
$CI->load->_ci_models[array_search($object$CI->load->_ci_modelstrue)]);
            }

            if (isset(
$CI->{$object})) {
                unset(
$CI->{$object});
            }
        }

        return 
$this;
    }
}

public function 
worker() {
    while (
true) { // infinite loop. The worker never dies.
        
$message $this->getMessage();

        if (!empty(
$messages)) {
            
$this->load->library('some_library');
            
$this->load->model('some_model');

            
// do something

            // destruct model and library and remove from CI super object, but keep CI super object alive.
            
$this->unloader->ci(['some_model''some_library']);
        }
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB