Jump to content

Working Around Interfaces With no Content


Cris987

Recommended Posts

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?

Link to comment
Share on other sites

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().

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.