Skip to content

Instantly share code, notes, and snippets.

@domzgarcia
Created October 5, 2018 08:13
Show Gist options
  • Save domzgarcia/4e5f1c9439616ad505ab2cfabc2aaba6 to your computer and use it in GitHub Desktop.
Save domzgarcia/4e5f1c9439616ad505ab2cfabc2aaba6 to your computer and use it in GitHub Desktop.
Simple OOP with Interface, Inheritance and static properties.
<?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