PHP- Object Oriented Programming
Object-oriented programming (OOP) in PHP allows you to organize your code into classes and objects, making it easier to manage and reuse. PHP supports OOP principles such as inheritance, encapsulation, and polymorphism. Here’s a comprehensive guide to OOP in PHP:
1. Classes and Objects
Defining a Class
A class is a blueprint for creating objects. It encapsulates data (properties) and behavior (methods).
php<?php
class Car {
// Properties
public $brand;
public $color;
// Constructor
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
// Method
public function start() {
return "Starting the $this->color $this->brand.";
}
}
?>
Creating Objects (Instances)
Objects are instances of classes. You create objects using the new
keyword.
php<?php
$myCar = new Car("Toyota", "Blue");
echo $myCar->start(); // Outputs: Starting the Blue Toyota.
?>
2. Constructors and Destructors
Constructors
Constructors are special methods called when an object is created. They initialize object properties.
php<?php
class Car {
public $brand;
public function __construct($brand) {
$this->brand = $brand;
}
}
$myCar = new Car("Honda");
echo $myCar->brand; // Outputs: Honda
?>
Destructors
Destructors are called when an object is destroyed or goes out of scope. They are useful for cleanup tasks.
php<?php
class Car {
public function __destruct() {
echo "Destroying object.";
}
}
$myCar = new Car();
unset($myCar); // Outputs: Destroying object.
?>
3. Properties and Methods
Properties
Properties are variables that hold data within objects. They can have different visibility (public, private, protected).
php<?php
class Car {
public $brand; // Public property
private $color; // Private property
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
}
?>
Methods
Methods are functions defined inside a class that perform actions related to the object.
php<?php
class Car {
public function start() {
return "Starting the car.";
}
public function stop() {
return "Stopping the car.";
}
}
?>
4. Inheritance
Inheritance allows a class (subclass) to inherit properties and methods from another class (parent class).
Parent Class
php<?php
class Vehicle {
public $brand;
public function __construct($brand) {
$this->brand = $brand;
}
public function start() {
return "Starting the $this->brand.";
}
}
?>
Subclass (Child Class)
php<?php
class Car extends Vehicle {
public function __construct($brand) {
parent::__construct($brand);
}
// Additional methods specific to Car
public function accelerate() {
return "Accelerating the $this->brand.";
}
}
$myCar = new Car("Toyota");
echo $myCar->start(); // Outputs: Starting the Toyota.
echo $myCar->accelerate(); // Outputs: Accelerating the Toyota.
?>
5. Encapsulation
Encapsulation means bundling the data (properties) and methods that operate on the data into a single unit (class). It helps in hiding the internal state of objects and restricting direct access to certain components.
php<?php
class BankAccount {
private $balance;
public function __construct($balance) {
$this->balance = $balance;
}
public function deposit($amount) {
$this->balance += $amount;
}
public function withdraw($amount) {
if ($amount <= $this->balance) {
$this->balance -= $amount;
} else {
echo "Insufficient funds.";
}
}
public function getBalance() {
return $this->balance;
}
}
$account = new BankAccount(1000);
$account->deposit(500);
echo $account->getBalance(); // Outputs: 1500
$account->withdraw(2000); // Outputs: Insufficient funds.
?>
6. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It allows flexibility in method implementation.
Example
php<?php
interface Shape {
public function area();
}
class Circle implements Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function area() {
return pi() * $this->radius * $this->radius;
}
}
class Rectangle implements Shape {
private $width;
private $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
public function area() {
return $this->width * $this->height;
}
}
$shapes = [
new Circle(5),
new Rectangle(3, 4)
];
foreach ($shapes as $shape) {
echo "Area: " . $shape->area() . "<br>";
}
?>