Friday, March 30, 2007

PHP: Calling Parent Class Constructors

For the past few weeks I have been working with PHP for a web application. With tons of annoyances, PHP can throw someone off balance if they are familiar with other languages.


One issue I came across, with tons of erroneous information regarding a solution, was a very simple issue of calling a parent classes constructor. For the below, I was using PHP 5.1.2 since I have been developing with PHPEclipse.


In this example, I have a base class, similar to this:


class BaseClass

{

private $_myValue;


function __construct()

{

$this->_myValue = new SomeClass();

}


}


Very simple, so I need this called in my extended class in order to initialize that myValue variable.


Now I saw some annoying solutions, which did not work, such as the below:

class DerivedClass extends BaseClass

{

private $_someOtherValuesNotInBase;


function __construct($initalizing)

{

$_ someOtherValuesNotInBase = $initalizing;

$this->BaseClass();

}


}


This didn’t work. PHP cried about not being able to find the method BaseClass(). Then I came across this little gem, which also did not work.


class DerivedClass extends BaseClass

{

private $_someOtherValuesNotInBase;


function __construct($initalizing)

{

$_ someOtherValuesNotInBase = $initalizing;

parent::BaseClass();

}


}


So, with all these erroneous solution, what is the correct answer? The following worked for me after some trial and error:


Class DerivedClass extends BaseClass

{

Private $_someOtherValuesNotInBase;


Function __construct($initalizing)

{

$_ someOtherValuesNotInBase = $initalizing;

$this->__construct();

}


}


5 comments:

Jason McIntosh said...

The more I use php, the more I think it's like interpretted java at times. The syntax has definitely been heading towards such a more java-like syntax. Look at the way they do class constructors, exception handling, etc.

On the other hand, for quick development of a website, it is amazing! A servlet can be a major pain to develop a quick form processing page, while php is easy. Further, php at times has supposedly out performed java in a number of enterprise areas, and so from a perspect of going for raw performance, it's possible php might be a better bet.

A note - I haven't used PHPEclipse - I'm still a gvim, subversion programmer. I'm surprised it didn't generate the constructor methods for you.

Ronny Stalker said...

Did you try calling the following line from within your derived class constructor?

--
parent::__construct();
--

John Ward said...

Sure did... didn't work :|

Maybe this was fixed in a newer version, due to some external project constraints I was forced to use 5.1.2.

bobobobo said...

I'm using parent::__construct, calling it as the first thing in the derived class

class DerivedClass extends BaseClass
{
private $_someOtherValuesNotInBase;

function __construct($initalizing)
{
parent::__construct();
$this->_someOtherValuesNotInBase = $initalizing;
}
}

also, don't forget $this-> before $_someOtherValuesNotInBase

Anonymous said...

parent::construct() ???