How does a code game work?

I'm having trouble understanding how icon loading loads.

Well, first you have a startup, which seems pretty straight forward, it loads everything every time. So it sounds good to use in the things that I use all the time.

Secondly, you can download all the built-in ones. But here is my question: how long does it stay loaded?

Let's say I load the form validation library into the controller, then load the model, can I use form validation in the model or do I need to reload it again? Continuing with, say, I load the view and another controller, can I use form validation? Or do I need to download? After redirecting? How about whether I load a model or helper instead of a library? Say I want to use a model inside another model, where can I load it?

So, the main question is, how long or sooner does it take to load before I need to restart?

+6
source share
3 answers

The download, as @yi_H correctly pointed out, lasts the entire current script lifetime. I.E. when you call the controller method, the resource is loaded. If you call the same resource inside another method, it is no longer available.

This is because the controller is initialized with every request, so when you access index.php/mycontroller/method1 controller is initialized (you can turn on the logs and see this clearly). In your method, you load, say, an html helper. If you then access index.php/mycontroller/method2, and you also need the html helper, but you did not load it into the intro method, you will get a function error not found.

So basically, if you want the same resource to always be available, you have 3 options:

  • load it into application / config / autoloader.php
  • load it with every request, that is, inside each method that this resource will use
  • place it inside the controller constructor so that it always initializes with every request.

This is more or less the same as autoload, except that it can only work for the controller in which you put the constructor, so you get the benefit when you don't want something loaded in the EACH controller (for example, when you use autoload), but only on a few. To use this last method, do not forget to WRITE the PARENT CONSTRUCTOR inside your controller (as usual with models):

 function __construct() { parent::__construct(); $this->load->library('whateveryouwant'); } 
+4
source

It stays there until the end of time (i.e. when your script ends and the universe collapses)

0
source

To load something while writing your own model or helper, for example:

 $ci = get_instance(); $ci->load->library('user_agent'); $ci->load->database(); 

For all other issues, I think you should download what you need for each controller.

-1
source

Source: https://habr.com/ru/post/892383/


All Articles