php 4 and 5 constructors
Moderator: General Moderators
-
hempheastus
- Forum Newbie
- Posts: 3
- Joined: Wed Feb 28, 2007 7:15 pm
php 4 and 5 constructors
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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
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.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Re: php 4 and 5 constructors
None really.hempheastus wrote:So my questions are:
1) What is the advantage to using the __constructor() syntax, over "class name" syntax?
Yes, just use the classname there isn't any speed difference under PHP5 between the two.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?
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.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?)
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.
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.)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?
Honestly, in my opinion, PHP5 doesn't bring that much to the table that is really exciting or useful when it comes to OOP.
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Re: php 4 and 5 constructors
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.hempheastus wrote: 1) What is the advantage to using the __constructor() syntax, over "class name" syntax?
What makes you think you still need to support php4?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?
See answer 1. Also, python and ruby, two biggest php competitors, both use "anonymous" constructors3) 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?)
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.php4) (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?
Hope that helped.
- dreamscape
- Forum Commoner
- Posts: 87
- Joined: Wed Jun 08, 2005 10:06 am
- Contact:
Re: php 4 and 5 constructors
Amen. The ability to do parent::__construct() was a godsend.stereofrog wrote: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.hempheastus wrote: 1) What is the advantage to using the __constructor() syntax, over "class name" syntax?
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
dreamscape wrote:Amen. The ability to do parent::__construct() was a godsend.stereofrog wrote: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.hempheastus wrote: 1) What is the advantage to using the __constructor() syntax, over "class name" syntax?
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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 
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.
Code: Select all
parent::doSomething();Code: Select all
super.doSomething();Code: Select all
parent::__construct();Code: Select all
super();- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Re: php 4 and 5 constructors
Let's see...stereofrog wrote:What makes you think you still need to support php4?
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.
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Re: php 4 and 5 constructors
If your host still has no support for php5, it's about time to change the host.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.
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.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.
Automatic object referencing, exceptions, overloading, interfaces, access modifiers - which of these features would you classify as "esoteric"?Especially when there is absolutely no need to do so except for really esoteric reasons.
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.
- neel_basu
- Forum Contributor
- Posts: 454
- Joined: Wed Dec 06, 2006 9:33 am
- Location: Picnic Garden, Kolkata, India
sourceforge.net Supports Only php4The 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.