Jump to content
boyjarv

classes, interfaces and methods

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

Share this post


Link to post
Share on other sites

What reference materials have you read to learn about each of these tools? 

Share this post


Link to post
Share on other sites

An Interface does not "call" anything. I think the manual description is pretty clear what an Interface does.

Quote

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are implemented.

Source: http://php.net/manual/en/language.oop5.interfaces.php

Share this post


Link to post
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();

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Other than not following the convention of using upper case for constant names, those declarations look fine. See manual

Share this post


Link to post
Share on other sites
5 minutes ago, Barand said:

Other than not following the convention of using upper case for constant names, those declarations look fine. See manual

Yeah, it's correct but somehow it feels like JavaScript - it's probably because I forgot you don't use dollar signs on constants...

Share this post


Link to post
Share on other sites

That syntax

const MYCONST = "xyz";

does feel a little strange after years of using

define("MYCONST", "xyz");

 

Share this post


Link to post
Share on other sites

There's that, too - plus I've been trying to bone up on my ESNext skills over the past month or so, so that's kinda where my head is right now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.