Created
October 5, 2018 08:13
-
-
Save domzgarcia/4e5f1c9439616ad505ab2cfabc2aaba6 to your computer and use it in GitHub Desktop.
Simple OOP with Interface, Inheritance and static properties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface IAnimal { | |
public function setSound($sound); | |
public function getSound(); | |
} | |
class Animal implements IAnimal { | |
protected static $sound; | |
protected $type; | |
protected $name; | |
public function __construct() | |
{ | |
static::$sound = 'Default sound'; | |
} | |
public function setSound($sound) | |
{ | |
self::$sound = $sound; | |
} | |
public function getSound() | |
{ | |
return self::$sound; | |
} | |
public static function mySound() | |
{ | |
return static::$sound; | |
} | |
} | |
class Dog extends Animal { | |
public function __construct() | |
{ | |
$this->name = 'Doggie'; | |
} | |
} | |
class Cat extends Animal { | |
public function __construct() | |
{ | |
$this->name = 'Mingming'; | |
} | |
} | |
$cat = new Cat(); | |
$cat->setSound('Meow meow'); | |
echo $cat->getSound(); | |
$dog = new Dog(); | |
$dog->setSound('Aw aw aw'); | |
echo $dog->getSound(); | |
echo ' -retain- ' . $dog::mySound(); | |
echo '<pre>', var_dump($dog); | |
echo '<pre>', var_dump($cat); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment