Content of
index.php
file
spl_autoload_register(function($class) {
    // echo 'register class:'.$class."<br>";
    require_once("classes/{$class}.php");
});

// echo 'hey there on line 7'."<br>";

$cat = new Cat;
$dog = new Dog;
$tortoise = new Tortoise;

echo $cat->talk();
echo $dog->talk();
echo $tortoise->talk();
Content of
classes/Talkative.php
file
interface Talkative {
    public function talk();
}
Content of
classes/Cat.php
file
class Cat implements Talkative {
    public function talk() {
        return 'Meow' . '<br>';
    }
}
Content of
classes/Dog.php
file
class Dog implements Talkative {
    public function talk() {
        return 'Woof' . '<br>';
    }
}
Content of
classes/Tortoise.php
file
class Tortoise implements Talkative {
    public function talk() {
        return 'Yak yak yak yak ...' . '<br>';
    }
}
Filenames and class names must be THE SAME
by Valeri Tandilashvili
4 years ago
PHP
OOP
PHP Object Oriented Programming (OOP)
1
Pro tip: use ```triple backticks around text``` to write in code fences