How can one efficiently extend the types defined in a PHP class without compromising performance or readability?

When extending types in a PHP class, it is important to use inheritance to avoid code duplication and maintain readability. To efficiently extend types without compromising performance, you can utilize interfaces to define common behavior and traits to share methods across multiple classes. This approach allows for better organization of code and promotes code reusability without sacrificing performance.

interface Animal {
    public function eat();
    public function sleep();
}

trait CanFly {
    public function fly() {
        echo "Flying!\n";
    }
}

class Bird implements Animal {
    use CanFly;

    public function eat() {
        echo "Eating seeds\n";
    }

    public function sleep() {
        echo "Bird is sleeping\n";
    }
}

class Dog implements Animal {
    public function eat() {
        echo "Eating bones\n";
    }

    public function sleep() {
        echo "Dog is sleeping\n";
    }
}

$bird = new Bird();
$bird->eat();
$bird->fly();
$bird->sleep();

$dog = new Dog();
$dog->eat();
$dog->sleep();