tags:

views:

252

answers:

7

Aside from the "you won't have to change the name", is there any real problems with retaining the PHP4 style constructor names:

ClassName($args)

in PHP5?

A: 

I think PHP5 constructors just allow you to work with the PHP more freely (allowing for more options).

dusoft
sorry that is a non answer -1
Byron Whitlock
whatever, but that's the exact answer - you can do more in PHP5, because there are more options to create object.
dusoft
A: 

No. Besides that it's ugly-ish (imo) and takes away one possible function name. ^^

Franz
+1  A: 

According to the documentation, that's the only drawback.

Jordan Ryan Moore
+2  A: 

For PHP5, there's no problem with retaining it. However, if the class has a __construct() function, it will be called instead.

Jason
+8  A: 

Because named constructors were only retained in PHP5 for backwards compatibility with PHP4, they are more likely to become deprecated in a later release of PHP than __construct() is, so that's something to keep in mind if you're writing something that you intend to have longevity.

Daniel Vandersluis
One of the goals with PHP 5 was added consistency. PHP is moving towards having code that is automatically invoked by the engine prefixed with __. We see this with new class options __construct, destruct, call, get, set, sleep, etc. We also see with __autoload. Using parent::__construct() also works when you use the PHP 4 style constructor, which can be a bit confusing.
preinheimer
+4  A: 

If you use the old style constructor names, you're creating a bit of confusion around situations like this

class A
{
    public function A()
    {
    }
}

class B extends A
{
    public function B()
    {
        //how do/should I call the parent constructor?
        //parent::A(); ?
        //parent::__construct(); ?
    }
}

If someone sees the old style constructor and switches it to a less-old style constructor, you risk functionality breaking. Also, if you have to juggle your object heirarchy so that B extends a different class, you need to manually change all the calls to parent::A(), or someone not familiar with the "constructor is a method with the class name" construct (meaning anyone who learned PHP in the last 5 years) may not know to do that and subtle breaking will occur.

Alan Storm
A: 

in php 4 oop is more a hack than everything else here is a code snipped for a constructor in php 5 style but its compatible for php 4

class Object
{
    function Object()
    {
        $args= func_get_args();
        call_user_func_array
        (
            array(&$this, '__construct'),
            $args
        );
    }

    function __construct()
    {
        // Abstrakt Funktion
    }
}
streetparade