Welcome Guest, Not a member yet? Register   Sign In
w/c is better? initiating libraries / models in class or in function?
#1

[eluser]upstream[/eluser]
Hi,

i normally do this way of initializing common libraries/models/helper used in a controls
Code:
public function index()
    {
        $this->load->model('my_custom_model','custom');

But i just found another way
Code:
class Hello world extends Controller
{
    private $libraries = array();
    private $helpers = array();
    
    public function __construct()
    {
        parent::__construct();
        $this->load->library($this->libraries);
        $this->load->helpers($this->helpers);
        $this->load->model('my_custom_model','custom');
    }
is this advisable? any drawbacks?

note:i prefer loading these common functionality within the controls and not in config file for portability of the code.

Thanks for answering Big Grin
#2

[eluser]Cro_Crx[/eluser]
Loading your libraries within the constructor instead of individual functions is fine. The only drawback is that the loads will be called for every single function within that controller (which may not be desirable).

So if only 1 out of 10 functions require a library for that particular controller this would be less efficient. Personally i use the autoload file to load any dependencies (to keep my controllers simple) although calling the loads within the constructor also cuts down on the amount of code you need.




Theme © iAndrew 2016 - Forum software by © MyBB