Inheritance in PHP:
Inheritance is a fundamental concept in object-oriented programming (OOP) where a class inherits properties and methods from another class. In PHP, this is achieved using the extends
keyword. Let's consider an example:
// Parent class
class Animal {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function speak() {
echo "{$this->name} speaks";
}
}
// Child class inheriting from Animal
class Dog extends Animal {
public function wagTail() {
echo "{$this->name} wags tail";
}
}
// Creating instances
$animal = new Animal("Generic Animal");
$dog = new Dog("Buddy");
// Calling methods
$animal->speak(); // Output: Generic Animal speaks
$dog->speak(); // Output: Buddy speaks
$dog->wagTail(); // Output: Buddy wags tail
?>
In this example, the Dog
class inherits the name
property and the speak()
method from the Animal
class.
Encapsulation in PHP:
Encapsulation refers to the bundling of data and methods that operate on the data into a single unit, i.e., a class. Access to the data and methods is typically restricted to prevent misuse. In PHP, encapsulation is achieved using access modifiers like public
, protected
, and private
. Let's see an example:
class BankAccount {
private $balance;
public function __construct($initialBalance) {
$this->balance = $initialBalance;
}
public function deposit($amount) {
$this->balance += $amount;
}
public function withdraw($amount) {
if ($this->balance >= $amount) {
$this->balance -= $amount;
return $amount;
} else {
return "Insufficient funds";
}
}
public function getBalance() {
return $this->balance;
}
}
// Creating an instance
$account = new BankAccount(1000);
// Accessing methods
echo "Current Balance: $" . $account->getBalance(); // Output: Current Balance: $1000
$account->deposit(500);
echo "<br>";
echo "After Deposit: $" . $account->getBalance(); // Output: After Deposit: $1500
echo "<br>";
echo "Withdrawn: $" . $account->withdraw(200); // Output: Withdrawn: $200
echo "<br>";
echo "Final Balance: $" . $account->getBalance(); // Output: Final Balance: $1300
?>
In this example, the balance
property is encapsulated within the BankAccount
class, and access to it is controlled through public methods like deposit()
, withdraw()
, and getBalance()
.
Polymorphism in PHP:
Polymorphism allows objects of different classes to be treated as objects of a common superclass. This allows for flexibility in programming. In PHP, polymorphism is often achieved through method overriding. Let's illustrate this:
// Parent class
class Shape {
public function calculateArea() {
return 0;
}
}
// Child classes
class Circle extends Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function calculateArea() {
return pi() * $this->radius * $this->radius;
}
}
class Rectangle extends Shape {
private $length;
private $width;
public function __construct($length, $width) {
$this->length = $length;
$this->width = $width;
}
public function calculateArea() {
return $this->length * $this->width;
}
}
// Creating instances
$circle = new Circle(5);
$rectangle = new Rectangle(4, 6);
// Calling the same method name on different objects
echo "Circle Area: " . $circle->calculateArea(); // Output: Circle Area: 78.539816339745
echo "<br>";
echo "Rectangle Area: " . $rectangle->calculateArea(); // Output: Rectangle Area: 24
?>
In this example, both the
Circle
and Rectangle
classes have a calculateArea()
method, but each class implements it differently. When calling calculateArea()
on objects of these classes, polymorphism ensures that the appropriate implementation is used based on the object type. Practice Excercise Practice now