• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to destruct libraries/models from CI base class?

#1
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 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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.