CodeIgniter Forums
Can I destroy Codeigniter library? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Can I destroy Codeigniter library? (/showthread.php?tid=86742)



Can I destroy Codeigniter library? - CarmosKarrvx - 02-15-2023

I'm wondering if there's a way to destroy an instance of a Codeigniter library. I want to free up RAM memory while running a large script, so I need to find a developerbook chatrandom way to destroy the library instance. Is it possible to do something like this in the code:
$this->load->library('my_library');
/**
Code goes here
**/
$this->my_library->destroy_instance();

I'd really appreciate any help on this matter.


RE: Can I destroy Codeigniter library? - InsiteFX - 02-16-2023

Unless you clean up the html code in your posts no one will bother to try and help you!


RE: Can I destroy Codeigniter library? - JackToncatridr - 02-20-2023

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

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);
}
}
In your controller ..

$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');
Hope it will be useful.