Use PHP inheritance (extends), interfaces (implements), abstract classes, parent::, method overriding, and polymorphism to build extensible class hierarchies.
PHP supports single inheritance (one parent class) but multiple interface implementation. Combined with traits (Lab 7), this gives flexibility without the complexity of multiple inheritance. PHP's OOP powers Laravel's Eloquent (model inheritance), Symfony's event system, and virtually every PHP framework.
35 minutes
Docker image: zchencow/innozverse-php:latest
Lab Instructions
Step 1: extends & Method Overriding
💡 static::class uses late static binding — it returns the actual runtime class, not the class where the method is defined. self::class would always return Animal. Use static:: when subclasses should see their own class name.
📸 Verified Output:
Step 2: Abstract Classes
💡 final public function generate() prevents subclasses from overriding the algorithm structure. Abstract methods force subclasses to provide implementations. This is the Template Method Pattern — one of the most used patterns in PHP frameworks.
📸 Verified Output:
Step 3: Interfaces
💡 Programming to interfaces (Payable $method) means processPayment works with any class implementing Payable — credit cards, PayPal, Bitcoin, gift cards. You can add new payment methods without changing processPayment. This is the Dependency Inversion Principle.
📸 Verified Output:
Step 4: Polymorphism — Shapes & Area
💡 Polymorphism means $s->area() calls the right method for each shape type without any if/switch. Adding a Pentagon class requires zero changes to describe(), the sorting, or the total calculation. This extensibility is the core benefit of OOP.
📸 Verified Output:
Step 5: final Classes & Methods
💡 final class prevents extension — use it for value objects, utility classes, and security-sensitive code where you want to guarantee behavior. final methods in non-final classes allow extension of the class but lock down specific methods. This is how PHP's DateTimeImmutable prevents accidental subclass mutations.
📸 Verified Output:
Step 6: Iterator Interface
💡 Implementing Iterator makes your class work in foreach, iterator_to_array(), and any function that accepts an iterable. This is how Laravel's Collection, Doctrine's result sets, and PHP's SPL data structures work — they're all iterators under the hood.
📸 Verified Output:
Step 7: Comparable Pattern & Sorting
📸 Verified Output:
Step 8: Complete — Payment System
📸 Verified Output:
You've applied PHP inheritance, abstract classes, interfaces, polymorphism, final, the Iterator interface, Comparable pattern, and a full payment system. These patterns underpin every PHP framework — Laravel's Eloquent, Symfony's event dispatcher, and Doctrine's collections all use these exact concepts.
Further Reading