Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code to manipulate that data. In PHP, OOP allows for modular, reusable, and maintainable code.
PHP Object-Oriented Programming (OOP)
1. Key Concepts of OOP
There are four main concepts of OOP:
- Classes: Blueprints for creating objects. A class defines properties (attributes) and methods (functions) that the objects created from the class will have.
- Objects: Instances of classes. An object is created from a class and can hold data and perform actions defined by its class.
- Inheritance: The ability of a class to inherit properties and methods from another class. This promotes code reuse.
- Encapsulation: The bundling of data (attributes) and methods (functions) that operate on the data into a single unit (class). It restricts direct access to some of the object's components, which is a means of preventing accidental interference and misuse of the methods and data.
2. Creating a Class and Object
To create a class in PHP, use the class
keyword followed by the class name. To create an object, use the new
keyword.
<?php
class Car {
// Properties
public $color;
public $model;
// Method
public function displayInfo() {
return "Car model: " . $this->model . ", Color: " . $this->color;
}
}
// Creating an object
$myCar = new Car();
$myCar->color = "Red";
$myCar->model = "Toyota";
echo $myCar->displayInfo(); // Output: Car model: Toyota, Color: Red
?>
3. Inheritance
Inheritance allows a class to use the properties and methods of another class. The class that inherits is called a child class, while the class it inherits from is called a parent class.
<?php
class Vehicle {
public $brand;
public function honk() {
return "Beep!";
}
}
class Car extends Vehicle {
public function displayBrand() {
return "Brand: " . $this->brand;
}
}
// Creating an object of the child class
$myCar = new Car();
$myCar->brand = "Honda";
echo $myCar->displayBrand(); // Output: Brand: Honda
echo $myCar->honk(); // Output: Beep!
?>
4. Encapsulation
Encapsulation is achieved by using access modifiers: public
, protected
, and private
.
<?php
class BankAccount {
private $balance;
public function __construct($initialBalance) {
$this->balance = $initialBalance;
}
public function deposit($amount) {
$this->balance += $amount;
}
public function getBalance() {
return $this->balance;
}
}
$account = new BankAccount(100);
$account->deposit(50);
echo $account->getBalance(); // Output: 150
?>
5. Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon. It is often achieved through method overriding.
<?php
class Animal {
public function makeSound() {
return "Some sound";
}
}
class Dog extends Animal {
public function makeSound() {
return "Woof!";
}
}
class Cat extends Animal {
public function makeSound() {
return "Meow!";
}
}
function animalSound(Animal $animal) {
return $animal->makeSound();
}
$dog = new Dog();
$cat = new Cat();
echo animalSound($dog); // Output: Woof!
echo animalSound($cat); // Output: Meow!
?>
6. Conclusion
Object-Oriented Programming in PHP provides a powerful way to organize and structure code. By understanding classes, objects, inheritance, encapsulation, and polymorphism, you can write more maintainable and reusable code.