Jump to content

classes, interfaces and methods


boyjarv

Recommended Posts

Hi, I am trying to get my head around classes, interfaces and methods for different animals

 

this is what I have so far:

<?php 
interface animal {
  function walk();
  function fly();
  function swim();
}
class monkey implements animal{
  const interface1 = "I am from test class";

  function noise() {
    echo "ooh ooh ah ah!";
  } 

  function walk() { 
    echo "monkey is walking";
  }
  function fly() { 
    echo "monkey is Flying";
  }
  function swim() { 
    echo "monkey is Swimming";
  }
  public function display() {
    echo monkey::interface1;
    echo PHP_EOL;
  }

}

class bear implements animal{
  const interface1 = "I am from test class";

  function noise() {
    echo "Grrr!";
  } 

  function walk() { 
    echo "Bear is walking";
  }
  function fly() { 
    echo "Bear is Flying";
  }
  function swim() { 
    echo "Bear is Swimming";
  }
  public function display() {
    echo bear::interface1;
    echo PHP_EOL;
  }

}
$Obj = new monkey();
$Obj->display();
$Obj1 = new bear();
$Obj1->display();
?>

I'm a bit stumped as  animal interface not calling all functions?! please help

Link to comment
Share on other sites

Is this close to what you are trying to do?

interface animal 
{
    
    function noise();
    function action();
}

class beast implements animal
{
    function noise() 
    {
        echo "Silence<br>"; 
    }
    function action() 
    {
        echo "Does nothing<br>"; 
    }
    function display()
    {
        $this->action();
        $this->noise();
        echo "<hr>";
    }
}

class bear extends beast 
{
    function noise()
    {
        echo "Grrrrr<br>";
    }
    
    function action()
    {
        echo "The bear is running<br>";
    }
}

class bird extends beast
{
    function noise()
    {
        echo "Tweet tweet<br>";
    }
    
    function action()
    {
        echo "The bird is flying<br>";
    }
}

$obj = new bird;
$obj->display();

$obj = new bear;
$obj->display();

 

Link to comment
Share on other sites

1 hour ago, boyjarv said:

I'm a bit stumped as  animal interface not calling all functions?! please help

Why are you stumped? You're not calling the methods. @Barand beat me to it, but here's another take on what he is saying.

interface animal{
	public function walk();
	public function fly();
	public function swim();
}

class Cat implements animal{
	public function walk(){
		echo "Cat can walk";
	}
	public function fly(){
		echo "Cat no fly";
	}
	public function swim(){
		echo "Cat no like";
	}
}

class Dog implements animal{
	public function walk(){
		echo "Dog loves walk!";
	}
	public function fly(){
		echo "Dog doesn't fly...";
	}
	public function swim(){
		echo "Dog LOVES swim!!!!!!!";
	}
}

class Animals{
	private $_animal;
	public function __construct(animal $animal){
		$this->_animal = $animal;
	}
	public function display(){
		$this->_animal->walk();
		$this->_animal->fly();
		$this->_animal->swim();
	}
}

$cat = new Animals(new Cat());
$dog = new Animals(new Dog());

$cat->display();
$dog->display();

Also, it almost looks like you're mixing PHP and JavaScript - in PHP all variables are prepended with a dollar sign. And the use of const is possible in PHP, but the way you've used it here looks a bit more like JavaScript to me.

Edited by maxxd
tpyo
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.