PHP Inline class definition, no loading required

Download PHP for free


// zoo.php
class Animal {
    public function eats($food) {
        echo "Yum, $food!";

$animal = new Animal();

PHP knows what Animal is before executing new Animal, because PHP reads source files top-to-bottom. But what if we wanted to create new Animals in many places, not just in the source file where it's defined? To do that, we need to load the class definition.

Manual class loading with require