aross
aross

Reputation: 3606

How to override the constructor of an extended class with a namespaced class (old-style constructor)?

Some PHP built-in classes use the old naming scheme for their constructor (I.E. their class name). SoapVar's constructor for example is Soapvar::SoapVar(). Now I'm trying to extend that class and override its constructor. I have to use the class name to do that, the usual __constructor() doesn't work.

Non-namespaced, using __constructor():

class SomeClass extends SoapVar {
  public function __constructor() {
    print "Trying to override parent's constructor.\n";
  }
}

$class = new SomeClass(); /* Not working */

Non-namespaced, using class name as constructor:

class SomeClass extends SoapVar {
  public function SomeClass() {
    print "Trying to override parent's constructor.\n";
  }
}

$class = new SomeClass(); /* Working */

Now I need to use a namespaced class. The only problem is this: 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. So this:

namespace Example\Namespace;

class SomeClass extends \SoapVar {
  public function SomeClass() {
    print "Trying to override parent's constructor.\n";
  }
}

$class = new SomeClass(); /* Not working */

doesn't work! Is there any way to solve this or am I forced to use the global namespace? Is there a bug report somewhere for this issue?

Upvotes: 0

Views: 179

Answers (1)

aross
aross

Reputation: 3606

Facepalm

Should be using __construct() and not __constructor()

Upvotes: 1

Related Questions