Unit 3 Notes
Unit 3 Notes
Inheritance is a key feature of Object-Oriented Programming (OOP) in PHP that allows a class
(child class) to inherit properties and methods from another class (parent class). This helps in
code reusability and hierarchy structuring.
1. Basic Syntax
In PHP, inheritance is implemented using the extends keyword.
php
CopyEdit
<?php
// Parent class
class Animal {
public $name;
interface Pet {
public function beFriendly();
}
trait CanSwim {
public function swim() {
echo "Swimming in water!";
}
}
class Bird {
use CanFly;
}
class Fish {
use CanSwim;
}
Summary Table
Type of Inheritance Supported in PHP? Description
Single Inheritance ✅ Yes One class inherits from another
Multilevel
✅ Yes A class inherits from another inherited class
Inheritance
Hierarchical
✅ Yes Multiple classes inherit from a single class
Inheritance
One class inherits from multiple classes (Not
Multiple Inheritance ❌ No (✅ Using Interfaces)
supported directly)
❌ No (✅ Using Traits &
Hybrid Inheritance A mix of multiple and hierarchical inheritance
Interfaces)
Polymorphism in PHP
Polymorphism is one of the key concepts of Object-Oriented Programming (OOP) that allows
objects of different classes to be treated as objects of a common superclass. In PHP,
polymorphism can be achieved using method overriding, interfaces, and abstract classes.
// Using polymorphism
$animals = [new Dog(), new Cat()];
// Polymorphic behavior
function makeAnimalSound(Animal $animal) {
$animal->makeSound();
}
// Polymorphic function
function printArea(Shape $shape) {
echo "Area: " . $shape->calculateArea() . "<br>";
}
printArea(new Circle(5)); // Output: Area: 78.5
printArea(new Rectangle(4, 5)); // Output: Area: 20
?>
Here, the printArea() function works with any object that implements the Shape interface.
5. Summary
Polymorphism
Description Example
Type
Method
Child class overrides a parent method makeSound() in Dog and Cat
Overriding
Abstract Classes Force child classes to implement methods Animal class with makeSound()
Allow multiple classes to follow the same Shape interface with
calculateArea()
Interfaces method structure
5. Summary
Method Description
__call() Handles calls to undefined or overloaded instance methods
__callStatic() Handles calls to undefined or overloaded static methods
Default Parameters Simulates overloading by setting default values
Key Takeaways
✅ PHP does not support traditional method overloading.
✅ Overloading is handled dynamically using __call() and __callStatic().
✅ Default parameters and conditional logic can simulate method overloading.
✅ Overloading is useful for flexible function calls with varying parameters.
Advanced OOP Features in PHP
1. Abstract Classes
Abstract classes cannot be instantiated.
They force child classes to implement certain methods.
Example:
php
CopyEdit
<?php
abstract class Animal {
abstract public function makeSound();
interface Fuel {
public function refuel();
}
class User {
use Logger;
}
class Order {
use Logger;
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test(); // Output: B
?>
Key Points:
✅ self:: refers to the class where the method is defined.
✅ static:: refers to the class that calls the method.
Summary Table
Feature Description
Abstract Classes Enforce method implementation in child classes
Interfaces Define a contract for classes
Traits Allow code reuse across multiple classes
Static Methods Methods that belong to the class, not instances
Late Static Binding Uses static:: for correct method resolution