0

I am trying to add a method to a class by extending it and then to cast an object to my child extended class:

public class motherClass(){
  public function methodA(){}
}

public class childClass extends motherClass (){
  public function methodB(){}
}


$obj= Method that return an istance of motherClass
$objChild = (childClass) $obj;
$objChild->methodB();

Is possible to do this in PHP ? I need to instaziate the $objChild becouse I want to call some method from childClass in an object that belongs to motherClass.

user1066183
  • 2,444
  • 4
  • 28
  • 57
  • I do not really understand what you are trying to achieve. could you clarify? – Philip G Nov 21 '14 at 00:50
  • 1
    This is known as downcasting. And no, PHP cannot do this. See http://stackoverflow.com/questions/1721949/downcasting-in-php5 – Kai Nov 21 '14 at 00:56
  • Check out this question and answer, which is pretty similar to what you're asking: http://stackoverflow.com/questions/4080217/cast-the-current-object-this-to-a-descendent-class – Eric Ping Nov 21 '14 at 01:02
  • Ok, there is a work around? I really need to add some methods to a class by extending it. – user1066183 Nov 21 '14 at 09:40

0 Answers0