Page 1 of 2
php 4 and 5 constructors
Posted: Wed Mar 21, 2007 5:47 pm
by hempheastus
I was wondering a few things about the differences between PHP 4 and 5. This is nothing serious so don't straign yourselves over it.
First off as I understand it:
1) The default constructor name in PHP 4 is the same as the "class name".
2) While the default constructor name in PHP 5 is __constructor().
3) But, PHP 5 is backwords compatible with PHP 4, and PHP 5 will understand the "class name" constructor if no __constructor() is found.
4) Also, PHP 4 will not recognize the __constructor() syntax, and if seen will just assume there is no constructor.
So my questions are:
1) What is the advantage to using the __constructor() syntax, over "class name" syntax?
2) For making code compliant with both PHP 4 and PHP 5 should I just use the "class name" syntax, or should I have a "class name" syntax for PHP 4 compatibility and a __construct() syntax which points to the "class name" function() for PHP 5 compatibility?
3) Why did they choose to change the syntax style? (Most programming languages I am familiar with use the "class name" style, so why do they want to be different?)
4) (This question goes beyond the scope of just constructors) In all of the differences between PHP 4 and PHP 5 that I have looked at, I have found that PHP 5 supports the PHP 4 standards. So is there any reason for using PHP 5 standards over the PHP 4 standards (speed, etc.)? Also, is there any instances where the PHP 5 standard does not support the PHP 4 standard?
Anyways thanks for any light yall can shed on this subject.
Posted: Wed Mar 21, 2007 6:03 pm
by Luke
I don't have a whole lot of time to answer this, so I'll just point out that it's __construct, not __constructor

Posted: Wed Mar 21, 2007 6:05 pm
by Chris Corbyn
I'd go with __construct() proxying the call the ClassName() but in response to the "why?" question, I've wondered this myself too.
Other large OO languages just use the class name (even C++ uses the classname prefixed with "~" for destructors). I can only assume it made it faster for PHP to deal with internally and also made it easier to override the constructor so that just like you'd called super() in java to run a parent constructor you need to use the "parent" keyword in PHP5. I'm just guessing though.
Re: php 4 and 5 constructors
Posted: Wed Mar 21, 2007 6:57 pm
by AKA Panama Jack
hempheastus wrote:So my questions are:
1) What is the advantage to using the __constructor() syntax, over "class name" syntax?
None really.
hempheastus wrote:2) For making code compliant with both PHP 4 and PHP 5 should I just use the "class name" syntax, or should I have a "class name" syntax for PHP 4 compatibility and a __construct() syntax which points to the "class name" function() for PHP 5 compatibility?
Yes, just use the classname there isn't any speed difference under PHP5 between the two.
hempheastus wrote:3) Why did they choose to change the syntax style? (Most programming languages I am familiar with use the "class name" style, so why do they want to be different?)
The only reason I can think of for them to make that change is to have a companion to __destruct() that looks similar. You can even simulate the __destruct() method under PHP4 using
register_shutdown_function. This will execute all functions in the order they were added to the register_shutdown_function. So if you need something special executed when the script finally finishes this will do it. This is also called BEFORE the objects are cleared from memory. This means you can register any destruct function inside an object so it will execute under PHP4/5 when the script ends.
Here is an example:
http://us3.php.net/manual/en/function.r ... .php#70968
BTW, in the example there is no need for the __contruct() function under PHP5. They could have removed it and placed the register_shutdown_function inside the classname function.
hempheastus wrote:4) (This question goes beyond the scope of just constructors) In all of the differences between PHP 4 and PHP 5 that I have looked at, I have found that PHP 5 supports the PHP 4 standards. So is there any reason for using PHP 5 standards over the PHP 4 standards (speed, etc.)? Also, is there any instances where the PHP 5 standard does not support the PHP 4 standard?
No, the only reason might be for the private variables and functions. These prevent anyone from accessing them from outside the scope of the object. But for most OOP uses under PHP these are not even needed. You can write a 100% PHP4 OOP class that will run under PHP5 without errors and perform most of the things in PHP5. (Note: Versions of PHP5 before 5.1.3 would throw a warning under E_STRICT if the class used VAR. This was fixed in PHP 5.1.3 and later.)
Honestly, in my opinion, PHP5 doesn't bring that much to the table that is really exciting or useful when it comes to OOP.
Posted: Wed Mar 21, 2007 9:52 pm
by feyd
PHP 5 complains if both __construct() and className() exist.
Re: php 4 and 5 constructors
Posted: Thu Mar 22, 2007 5:37 am
by stereofrog
hempheastus wrote:
1) What is the advantage to using the __constructor() syntax, over "class name" syntax?
There are two major advantages. First, more consistency, "parent::_construct" works everywhere, can be e.g. copypasted across classes. Second, refactoring. Renaming a class with classname constructor is a pain, because you must look for and replace all calls to the parent constructor in child classes.
2) For making code compliant with both PHP 4 and PHP 5 should I just use the "class name" syntax, or should I have a "class name" syntax for PHP 4 compatibility and a __construct() syntax which points to the "class name" function() for PHP 5 compatibility?
What makes you think you still need to support php4?
3) Why did they choose to change the syntax style? (Most programming languages I am familiar with use the "class name" style, so why do they want to be different?)
See answer 1. Also, python and ruby, two biggest php competitors, both use "anonymous" constructors
4) (This question goes beyond the scope of just constructors) In all of the differences between PHP 4 and PHP 5 that I have looked at, I have found that PHP 5 supports the PHP 4 standards. So is there any reason for using PHP 5 standards over the PHP 4 standards (speed, etc.)? Also, is there any instances where the PHP 5 standard does not support the PHP 4 standard?
php5 is mostly compatible with php4, however it adds many new useful features to the language, so there's many reasons to upgrade beyond "speed". For more details, see
http://www.php.net/manual/en/migration5.oop.php
Hope that helped.
Re: php 4 and 5 constructors
Posted: Thu Mar 22, 2007 7:10 am
by dreamscape
stereofrog wrote:hempheastus wrote:
1) What is the advantage to using the __constructor() syntax, over "class name" syntax?
There are two major advantages. First, more consistency, "parent::_construct" works everywhere, can be e.g. copypasted across classes. Second, refactoring. Renaming a class with classname constructor is a pain, because you must look for and replace all calls to the parent constructor in child classes.
Amen. The ability to do
parent::__construct() was a godsend.
I'd also say a 3rd advantage is forward compatibility. In PHP 5, the class name constructor is only supported for backward compatibility with PHP 4. PHP 6 is going to break a lot of compatibility with PHP 4, and while I cannot be 100% sure, I am fairly certain that we'll see the class name constructor support disappear with PHP 6.
Re: php 4 and 5 constructors
Posted: Thu Mar 22, 2007 8:11 am
by infolock
dreamscape wrote:stereofrog wrote:hempheastus wrote:
1) What is the advantage to using the __constructor() syntax, over "class name" syntax?
There are two major advantages. First, more consistency, "parent::_construct" works everywhere, can be e.g. copypasted across classes. Second, refactoring. Renaming a class with classname constructor is a pain, because you must look for and replace all calls to the parent constructor in child classes.
Amen. The ability to do
parent::__construct() was a godsend.
I'd also say a 3rd advantage is forward compatibility. In PHP 5, the class name constructor is only supported for backward compatibility with PHP 4. PHP 6 is going to break a lot of compatibility with PHP 4, and while I cannot be 100% sure, I am fairly certain that we'll see the class name constructor support disappear with PHP 6.
Before you read my point, please understand I'm a php 4 guy, and I have not migrated to 5.
That begin said, couldn't you still use parent::__construct() outside the class but still not use the __construct function in the class itself and just use the ClassName() constructor instead?
Ie:
Code: Select all
class bob {
function bob() {
}
}
class sally extends bob {
function __construct() {
parent::__construct();
}
}
isn't this still valid?
If so, then it doesn't matter which one you use in the parent at all. it's all about which ever floats your boat. the only time it will matter is when you get in deep heiarchy classes that you'll need to call the parent back, then php 5 will rock the free world and php 4 will still be a little slower =P
but either way, the parent::__construct() isn't exactly a godsend. I c an still say bob::bob() and get the same result. however, it does add a bit more ease to people's lives i guess.
Posted: Thu Mar 22, 2007 8:39 am
by Chris Corbyn
You wouldn't want to call bob::bob() since you've come out of context there, you'd need to call parent::bob().
Like I say, Java has the one keyword "super" which acts like "parent", except that you can directly place parens after it and it then becomes a construct:
i.e.
I guess they were just trying to standardise the way to call the super-constructor

Re: php 4 and 5 constructors
Posted: Thu Mar 22, 2007 9:57 am
by AKA Panama Jack
stereofrog wrote:What makes you think you still need to support php4?
Let's see...
How about that the vast majority of hosts only support support PHP 4. Those that do support PHP 5 usually, not always, include a hack to allow use at the same time as PHP4 and/or they use an older buggy version of PHP5.
If any developer wants to have the widest exposure possible for their applications, commercial or open source they will usually create a PHP4 OOP class that works on BOTH PHP4 and PHP5. Right now creating only a PHP5 class is cutting your own throat when it comes to gaining exposure and usage. Especially when there is absolutely no need to do so except for really esoteric reasons.
When over 60% of the hosting companies out there offer PHP5 instead of PHP4 then I will think about POSSIBLY creating PHP5 only code. Until then it is a not even an option.
Re: php 4 and 5 constructors
Posted: Thu Mar 22, 2007 10:49 am
by stereofrog
AKA Panama Jack wrote:
How about that the vast majority of hosts only support support PHP 4. Those that do support PHP 5 usually, not always, include a hack to allow use at the same time as PHP4 and/or they use an older buggy version of PHP5.
If your host still has no support for php5, it's about time to change the host.
If any developer wants to have the widest exposure possible for their applications, commercial or open source they will usually create a PHP4 OOP class that works on BOTH PHP4 and PHP5. Right now creating only a PHP5 class is cutting your own throat when it comes to gaining exposure and usage.
A really good and useful module will gain acceptance disregarding of the language version it's written in. Quite the contrary, carrying around a bunch of obsolete code can cost you some popularity.
Especially when there is absolutely no need to do so except for really esoteric reasons.
Automatic object referencing, exceptions, overloading, interfaces, access modifiers - which of these features would you classify as "esoteric"?
Posted: Thu Mar 22, 2007 10:59 am
by Luke
I'm getting tired of this "You must support PHP4 for your application to be popular because most hosts only have php4" stuff. It's a vicious circle. hosts support php4 because php4 is still being used by a lot of developers. developers still use php4 because hosts only support php4. when will it end%AC I think it's time to move on.
Posted: Tue Mar 27, 2007 4:15 am
by neel_basu
The Ninja Space Goat wrote:I'm getting tired of this "You must support PHP4 for your application to be popular because most hosts only have php4" stuff. It's a vicious circle. hosts support php4 because php4 is still being used by a lot of developers. developers still use php4 because hosts only support php4. when will it end%AC I think it's time to move on.
sourceforge.net Supports Only php4
Posted: Tue Mar 27, 2007 4:23 am
by Xoligy
I think PHP 6 will be the one that everyone jumps to. I know I shall be, as I can see many advantages where as PHP5 IMO the disadvantages way out the advantages.
Posted: Tue Mar 27, 2007 4:30 am
by neel_basu
Xoligy wrote:I think PHP 6 will be the one that everyone jumps to. I know I shall be, as I can see many advantages where as PHP5 IMO the disadvantages way out the advantages.
Although I've Downloaded It But I've Never Used It.I Think It Would Be Very Cool.