PHP4/5 constructor differences

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

PHP4/5 constructor differences

Post by Chris Corbyn »

Hi,

Anybody else do this or have a reason not to?

Code: Select all

class whateverClass {

    //PHP5's constructor
    function __construct() {
        //
    }

    //PHP4's constructor
    function whateverClass() {
        $this->__construct(); //Loop back into the PHP5 one
    }

}
EDIT | Corrected rushed typos
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

I don't understand why you would? Why would you use PHP 5 object syntax in PHP 4 application. PHP 5 should pick up on old constructors just fine. Did I miss something? :?:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Well I see how it allows the same script to run both places; but you still can take advantage of PHP5's features if you're trying for compatability with PHP4... so you still can use well-named super-class constuctors, etc...

So I don't see the point in this instance.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

The manual:
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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

neophyte wrote:The manual:
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.
That'll teach me to RTFM :P

I literally just started using PHP5 lol ;)
Post Reply