Organizational Research By

Surprising Reserch Topic

codeigniter constructors what is the difference


codeigniter constructors what is the difference  using -'php,codeigniter'

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

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


and

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


What is the difference between them? I'm not sure which to choose.
    

asked Sep 15, 2015 by EviGrenier
0 votes
5 views



Related Hot Questions

3 Answers

0 votes

If you are using Codeigniter 2+ (which you should be)... The second option will not work, as it uses the PHP4 style constructor calls.

Actually, the second option wouldn't work anyway because the php4 constructor call needs to match the class you're extending...

So yeah, use the first one. It uses PHP5 style constructor calls.

For more information on PHP5 constructors

answered Sep 15, 2015 by DanteI05ftk
0 votes

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.

answered Sep 15, 2015 by MitchelCasim
0 votes

php 5 has changed the way constructors are called and used and you should use the second option. I think this is not specific to codeigniter and general to php5

answered Sep 15, 2015 by JulLio

...