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

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

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

Without loading beforehand
@$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?

load them in the config/autoload.php file, by setting

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

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

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

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?

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.

@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-2021 MyBB Group.