get_instance() in Codeigniter: Why assign it to a variable?

后端 未结 7 1673
清酒与你
清酒与你 2020-12-04 08:42

In Codeigniter, get_instance() is a globally available function that returns the Controller super-object which contains all the currently loaded classes (it ret

7条回答
  •  独厮守ぢ
    2020-12-04 09:02

    As far as I know, it's a matter of convenience more than anything. Chances are that you will be using the CI super object a lot in your libraries so why not assign it to a variable to make it a little easier to work with?

    There are a few other things to consider...

    1. If you put this method in a helper, that method becomes a dependency for any class you are using it in. This might not be a big deal for you, but if you want to share libraries with anyone else they may not be happy about the dependency, especially since there is already a standard way of handling this in the CI community.
    2. There is a slight impact on performance because you are calling get_instance() every time you use the helper rather than storing its result in a variable.
    3. Since this is a helper method that is supposed to save you time, for anyone who is working mostly in the core MVC files of CI, setting up a helper like this would take longer than just setting it to a variable in the few places you need it.

提交回复
热议问题