Are destructors possible in Codeigniter 2.0?
Similar to building a class constructor in Codeigniter which performs a block of code before peforming other functions within a class:
<?php
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
}
}
?>
Is there a way to build a "destructor" function that properly processes a block of开发者_如何学C code after your called function is completed? It seems that this code does not process a "footer" inside of my app:
function __destruct() {
$this->load->view('footer');
}
function __destruct()
, called before object is destructed. And it isn't specific to CodeIgniter actually. Also keep in mind that if you leave your object alive to the very end of the script, so that it is destructed when PHP is shutting down there might be various problems. So if it's possible it might be a good idea to call unset($obj)
at the end of your script.
Update
Now after you added what were your intentions, I can say that it won't work:). Controller is destructed when the CI superclass is being destructed, i.e. after everything was dispatched and sent to client. As a quick alternative (if it's urgent) I can suggest making action methods private to route all action requests to __call
which will add header/footer to all your actions. But if it isn't urgent I'll make a better suggestion after I dig it a bit when I get to work...
精彩评论