Can I extend a class after the parent class is created?
Posted: Tue Dec 09, 2008 7:38 pm
I'm trying to extend a class after the parent class already is defined. Is that possible? If so, how do you do it? The functions that allow me to determine what object type is needed is inside the parent class.
Here's an example:
Here's an example:
Code: Select all
public class shape{
private $shapeData; //large object filled with data that is used later in other class functions
private $shapeType;
public __construct($urlToShapeData){
$this->shapeData=getXML($urlToShapeData) //function does not exist, but illustrates the point.
$shapeType = determineShapeType($this->shapeData);
}
private function determineShapeType(){
if($this->shapeType=="circle"){
//extend class to be a circle
}elseif($this->shapeType=="square"){
//extend class to be a square
}
}
abstract public function doStuff();
}
public class circle extends shape{
public function doStuff(){
//Do circle specific stuff
}
}
public class square extends shape{
public function doStuff(){
//Do square specific stuff
}
}
$shape=new shape(); //creates the parent, which is then automatically extended into the correct child class
$shape->doStuff();