• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading libaries, helpers, configs, models etc at call-time

#1
[eluser]micha8l[/eluser]
I was thinking, by way of overloading, is it possible/recommendable to load a model or something at call-time.

Standard codeigniter way:
Code:
$this->load->model(array('blog_model', 'blog_posts_model'));
$this->blog_model->do_something();


Without loading beforehand
Code:
@$this->blog_model->do_something(); // ??

I'm just trying to refactor my code by cutting down on maintenance and lines used. Please tell me if this is possible/been done/bad idea, etc, etc?

#2
[eluser]Killswitch[/eluser]
load them in the config/autoload.php file, by setting

Code:
$autoload['models'] = array('blog_model', 'blog_post_model');

#3
[eluser]micha8l[/eluser]
[quote author="Killswitch" date="1344387803"]load them in the config/autoload.php file, by setting

Code:
$autoload['models'] = array('blog_model', 'blog_post_model');
[/quote]

Thanks for replying man. I know about autoload, but this isn't what I'm trying to achieve. I'd like to side-step any loading entirely, and instead only load when called. Although I'm unaware of the implication this would cause, because surely if the idea was sound Codeigniter would of implemented it, right?

#4
[eluser]Aken[/eluser]
First, you can load models, libraries and such inside each other. If one model requires the other, you can load it inside the first model.

Second, there's no built-in autoload functionality that will load on calls. You would have to create a base controller and your own __get() method to load resources. You'll need your own system of specifying whether you're loading a model or library or what, otherwise __get() won't really know what to call. It has it's downfalls, but would be a suitable solution if developed properly.

#5
[eluser]PhilTem[/eluser]
@Aken is, again, right. There's no magic autoload functionality in CI. You have to extend CI_Loader in order to get that thing going Wink Or you would use something like spl_autoload_register added to CI_Loader so you will load models/libraries when they are called without prior loading.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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