explain $CI =& get_instance();

前端 未结 5 2059
长情又很酷
长情又很酷 2020-11-30 19:15

Looking through codeigniter\'s source code,

in its helper functions I keep seeing code $CI =& get_instance(); can anyone please explain to me how th

5条回答
  •  借酒劲吻你
    2020-11-30 19:42

    It's basically a Singleton Design Pattern that uses a function instead of a static method.

    To look deeper, check out the source code

    So basically, it doesn't enforce the singleton, but it's a shortcut to a public function...

    Edit: Actually, now I understand. For PHP4 compatibility they had to do a double-global-variable-hack to get it to return the references properly. Otherwise the references would get all screwed up. And since PHP4 didn't have support for static methods (well, properly anyway), using the function was the better way. So it still exists for legacy reasons...

    So if your app is PHP5 only, there should be nothing wrong with doing CI_Base::get_instance(); instead, it's identical...

提交回复
热议问题