oop - Cast derived class to base class in php -


i'm missing obvious here basic functionality in other oop languages, i'm struggling php way of doing this. understand php not "true" oop language, still...

what i'm after casting object instantiated derived class base class. along lines of:

class some{}  class somemock extends {}  function getsomemock(){   $mock = new somemock();   return (some)$mock; //syntax error here } 

i've found questions strange request of downcasting objects of base class derived possible nasty tweaks, basic functionality not have difficult. missing here?

edit: seems it's matter i'm trying achieve. no problem. getsomemock() factory method return mock object stub (derived base class, properties prepopulated in constructor) expected properties' values. compare object of base type restored database:

$this->assertequals($mockobject, $realobject); 

this fails instantly $mockobject , $realobject of different types. can imagine there many workarounds can implement achieve same, i'd keep things simple possible.

in php cannot cast specific class, cannot see need that. may cast native type - string, int, array, object. not specific class.

if need use functionality of base class, can via parent keyword.

class {   public function execute(){     echo "i some" ;   } }  class somemock extends {   public function execute(){ //override function     parent::execute() ; //here can execute parent's functionality , add more   } } 

edit:

instanceof operator may come handy. when comparing objects. instance:

$object = new somemock() ; $status = ($object instanceof && $object instanceof somemock); //will return true here ; 

child classes inherit non-private properties , methods.

let's say, have function:

function assertequals($mockobject, $realobject){   if ($mockobject instanceof && $realobject instanceof some){     //both objects have same base class -     //that means must have inherited functionality , properties of class.   } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -