Codeigniter constructors. What is the difference?

后端 未结 4 1132
轻奢々
轻奢々 2021-02-09 18:03

I see two types of contructors in CI. For instance...

class Blog extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }
}
         


        
4条回答
  •  被撕碎了的回忆
    2021-02-09 18:33

    Using a function with the name __construct() is the way constructors are written in PHP 5.

    Using a function that has the same name as the class is the way constructors were written in PHP 4 (and, for compatibility reasons, those still work in PHP 5 -- even if you should prefer __construct())


    As a reference, take a look at Constructors and Destructors -- quoting a portion of it :

    For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class.

提交回复
热议问题