6

should i use Classname() or __construct() as constructor in CodeIgniter?

both work, which should i use?

never_had_a_name
  • 90,630
  • 105
  • 267
  • 383
  • Possible duplicate of [\_\_construct() vs SameAsClassName() for constructor in PHP](http://stackoverflow.com/questions/217618/construct-vs-sameasclassname-for-constructor-in-php) – nawfal Nov 12 '15 at 06:50

2 Answers2

9

Classname() is the old way (i.e. PHP 4 way).

__construct() is the new (i.e. PHP 5) way.

You should use the second one, if your application is written with PHP 5 -- and you should write your applications with PHP 5 in mind !


See the Constructors and Destructors section in the manual, which states (quoting) :

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
0

ClassName() and __construct() is same that is known as constructor.

__construct() function is most useful comparison to className() because when you change your ClassName() you have to change your constructor name but no need to change __construct() and also use in child class.

j0k
  • 22,600
  • 28
  • 79
  • 90