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

后端 未结 7 1572
清酒与你
清酒与你 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:16

    It is necessary to assign by reference because the value of CI_Controller::$instance is subject to change if another instance of the class is created. The constructor re-assigns self::$instance each time it runs.

    In general, this feels like a bad design pattern and is missing the property of a singleton that limits the class to only one instance, http://en.wikipedia.org/wiki/Singleton_pattern.

    It seems possible to type, CI_Controller::get_instance()->$className->$method(); which does seem like more typing that your requested CI()->$className->$method.

    Ultimately, it would make sense to require that only one instance of $instance can be created and then the need for assigning by reference would be eliminated.

    0 讨论(0)
提交回复
热议问题