should i use Classname() or __construct() as constructor in CodeIgniter?
both work, which should i use?
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.
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.
Related
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what is the function __construct used for?
is there any difference between __construct function and function with same name as class has?
class foo {
function foo ($something){
echo "I see ".$something." argument";
}
}
class bar {
function __construct ($something){
echo "<br />
I see ".$something." argument again";
}
}
$foo = new foo("foo");
$bar = new bar("bar");
The method named is the PHP4 way of doing a constructor.
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. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.
http://www.php.net/manual/en/language.oop5.decon.php
Constructor function named same as class is a backward compatibility feature for PHP4. It will not work with namespaced classes since PHP 5.3.3
If both __construct and class-named functions are present, then the __construct is used as constructor.
The first one is old php4 style "construct". It is basically the same as the __construct.
The difference is that calling a constructor by the same name of the class is deprecated.
The difference is that PHP version 5.3.3 and above will treat function foo() as regular method and not constructor. Previous versions will treat it as a constructor.
init() method :
public function init()
{
}
__construct() method:
public function __construct()
{
}
So, what's the differentce between them, and which should be used?
init() is the method of any object that extends from yii\base\Object (and the majority of objects extends from it).
From official docs:
Besides the property feature, Object also introduces an important
object initialization life cycle. In particular, creating a new
instance of Object or its derived class will involve the following
life cycles sequentially:
the class constructor is invoked;
object properties are initialized according to the given configuration;
the init() method is invoked.
In the above, both Step 2 and 3 occur at the end of the class
constructor. It is recommended that you perform object initialization
in the init() method because at that stage, the object configuration
is already applied.
It's recommended to use init(), you can even see it from source code and the extensions, but in some cases, you can use __construct(). There are some recommendations to implement that, you can find it on the same page in official docs here.
__constuct is a native PHP language feature, you can read more info about that in PHP official docs in this section.
Before PHP 5.3.3 the following
class Same {
public function same() { echo 'Not good'; }
}
$c = new Same();
will output Not good.
From 5.3.3+ instead it will not output the string. That's because from PHP 5.3.3 functions with the same name of the class are not considered constructors.
How do I force this behavior even with PHP 5.3.2 or before?
The easiest way is probably just to create an empty constructor:
class Same {
public function same() { echo 'Not good'; }
public function __construct() { }
}
$c = new Same();
That won't echo "Not good" as the __construct() method overrides the "same name as class" method.
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. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
Unlike with other methods, PHP will not generate an E_STRICT level error message when __construct() is overridden with different parameters than the parent __construct() method has.
As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.
docs:- http://php.net/manual/en/language.oop5.decon.php
So obvious solution is to declare a __constructors method (even is an empty one)
The construct method is named __construct(). Simply call your same()-method inside __construct() if u wish to have the same name.
According to http://php.net/construct php tries to reserve backwards compatibility. In my opinion the "same" name means writing the method name case-sensitive (as the class name). That should work too.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what is the function __construct used for?
is there any difference between __construct function and function with same name as class has?
class foo {
function foo ($something){
echo "I see ".$something." argument";
}
}
class bar {
function __construct ($something){
echo "<br />
I see ".$something." argument again";
}
}
$foo = new foo("foo");
$bar = new bar("bar");
The method named is the PHP4 way of doing a constructor.
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. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.
http://www.php.net/manual/en/language.oop5.decon.php
Constructor function named same as class is a backward compatibility feature for PHP4. It will not work with namespaced classes since PHP 5.3.3
If both __construct and class-named functions are present, then the __construct is used as constructor.
The first one is old php4 style "construct". It is basically the same as the __construct.
The difference is that calling a constructor by the same name of the class is deprecated.
The difference is that PHP version 5.3.3 and above will treat function foo() as regular method and not constructor. Previous versions will treat it as a constructor.
I have recently started php and was wondering the differences between __construct() and having a method with the same name as the class?
Is there a reason for using it? All I can work out is it overrides the method named Foo or is it down to which you prefer?
E.g.
class Foo {
function Foo()
{
echo 'Foo stated<br>';
}
function __construct() {
echo 'Construct started<br>';
}
}
Thanks
Using __construct() is the newer PHP5 more OOP focused method to call a constructor. Using a method with the same name as the class is the old deprecated way to do it, and it will not function as a constructor as of PHP 5.3.3 for namespaced classes.
From the constructors and destructors page:
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. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
Unlike with other methods, PHP will not generate an E_STRICT level error message when __construct() is overridden with different parameters than the parent __construct() method has.
As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.
"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. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics."
Source: http://www.php.net/manual/en/language.oop5.decon.php
Foo() is a php4 way(deprecated), __construct() is php5 way. php will first look for __construct, then, if it's not found, it will use Foo
Is there a reason for using it? All I
can work out is it overrides the
method named Foo or is it down to
which you prefer?
The benefits of __construct() become clearer when you involve renaming and inheritance. If you rename a class, you then have to rename it's constructor. No big deal, but if class B inherits from class A you could end up with:
class B extends A {
function B() {
parent::A();
}
}
It's much easier and more maintainable to do:
function __construct() {
parent::__construct();
}
Now when you rename class A, you don't have to remember to also change the parent calls in all its children. Granted, there's still renaming to do in your code but at least this is not one of them.
Having a method with the same name as the class, which is automatically called as a constructor is a throwback to PHP4, and is to be considered deprecated... in the latest development branch of PHP, it will be treated as a normal class method. __construct is the formally accepted constructor in PHP5, although in current releases it will fall back to the PHP4 method if __construct doesn't exist.