boyjarv Posted December 4, 2018 Share Posted December 4, 2018 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 4, 2018 Share Posted December 4, 2018 What reference materials have you read to learn about each of these tools? Quote Link to comment Share on other sites More sharing options...
benanamen Posted December 4, 2018 Share Posted December 4, 2018 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted December 4, 2018 Share Posted December 4, 2018 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(); Quote Link to comment Share on other sites More sharing options...
maxxd Posted December 4, 2018 Share Posted December 4, 2018 (edited) 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 December 4, 2018 by maxxd tpyo Quote Link to comment Share on other sites More sharing options...
Barand Posted December 4, 2018 Share Posted December 4, 2018 Other than not following the convention of using upper case for constant names, those declarations look fine. See manual Quote Link to comment Share on other sites More sharing options...
maxxd Posted December 4, 2018 Share Posted December 4, 2018 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... Quote Link to comment Share on other sites More sharing options...
Barand Posted December 4, 2018 Share Posted December 4, 2018 That syntax const MYCONST = "xyz"; does feel a little strange after years of using define("MYCONST", "xyz"); Quote Link to comment Share on other sites More sharing options...
maxxd Posted December 4, 2018 Share Posted December 4, 2018 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. 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.