Cris987 Posted December 3, 2007 Share Posted December 3, 2007 I noticed just now that interfaces in PHP cannot contain concrete code. What if I have some classes under one superclass, most of which share the same function? Wouldn't it be tedious to copy the code over and over again? For e.g Class Animal Class Dog extend Animal Class Cat extends Animal Class Hiipo extends Animal Dogs and Cats can kill() in exactly the same way. Hiipo and Dogs can run() in the same way. Do I HAVE to copy and past the code for kill and run? Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2007 Share Posted December 3, 2007 You'll want to look at abstract classes. Quote Link to comment Share on other sites More sharing options...
Cris987 Posted December 3, 2007 Author Share Posted December 3, 2007 Sorry for putting this in the worng thread b4, anyways: How would abstract class work here? Only Dogs and Hippos can kill() and only Cat and Dogs could run() If I have an abstract class Animal w/ run() and kill() in it, and Cats, Dogs, and Hippos extend the Animal class, wouldn't it mean that they can all run and kill? One solution I think of now is to define run() and kill() in the Animal class and then override it with some form of useless function in Animals that aren't suppose to be able to run() or kill(). Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 4, 2007 Share Posted December 4, 2007 Rewriting the method is an idea. You could also set protected vars that say if the functionality is allowed class parent{ protected $_test = true; protected function test(){ if($this->_test){ ... } } class child extends parent{ public function __construct(){ this->_test = false; } } } now you can not use that test method if you're using the child class Quote Link to comment Share on other sites More sharing options...
aschk Posted December 7, 2007 Share Posted December 7, 2007 I have included an example of doing this utilising the strategy pattern. Read for yourself and work it out. <?php abstract class animal { public function setRunningBehaviour(runner $runner){ $this->runBehaviour = $runner; } public function run(){ $this->runBehaviour->run(); } public function setKillingBehaviour(killer $killer){ $this->killingBehaviour = $killer; } public function kill(){ $this->killingBehaviour->kill(); } } /** * Runner interface * */ interface runner { public function run(); } class nonRunner implements runner { public function run(){ echo "I can't run you idiot."; } } class fastRunner implements runner { public function run(){ echo "Running real fast (70mph)."; } } class slowRunner implements runner { public function run(){ echo "Lumbering along."; } } /** * Killer interface. * */ interface killer { public function kill(); } class nonKiller implements killer { public function kill(){ echo "I don't kill, i'm nice."; } } class brutalKiller implements killer { public function kill(){ echo "Slash, hack, hack, devour, gorge!"; } } /** * Concrete animals */ class hippo extends animal { public function __construct(){ $this->setKillingBehaviour(new nonKiller()); $this->setRunningBehaviour(new slowRunner()); } } class dog extends animal { public function __construct(){ $this->setKillingBehaviour(new brutalKiller()); $this->setRunningBehaviour(new fastRunner()); } } class cat extends animal { public function __construct(){ $this->setKillingBehaviour(new nonKiller()); $this->setRunningBehaviour(new fastRunner()); } } $cat = new cat(); $dog = new dog(); $hippo = new hippo(); echo "cat:"; $cat->kill(); $cat->run(); echo "dog:"; $dog->kill(); $dog->run(); echo "hippo"; $hippo->kill(); $hippo->run(); ?> Instead of echo'ing out "can't do blah" you could throw an exception (or extend exception and call it invalidAction and throw that) which you can then catch and deal with. So if someone attempted to kill() using a cat, you could catch that exception and say "BAD HUMAN, cats don't kill!" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.