Object Oriented in PHP Language
Object-Oriented Programming (OOP) is a fundamental concept in modern PHP development. It allows you to organize and structure your code
in a more modular and efficient way. In this post, we’ll provide an overview of OOP in PHP with examples to help you grasp its core principles.Understanding OOP in PHP Language
At its core, OOP is about modeling real-world entities and their interactions in your code. In PHP, everything is treated as an object, which is an instance of a class. Classes act as blueprints or templates for creating objects, defining their properties (attributes) and behavior (methods).
Example: Creating a Class
class Car {
public $brand;
public $model;
public function startEngine() {
echo "The $this->brand $this->model's engine is running.";
}
}
In this example, we’ve defined a Car
class with properties ($brand
and $model
) and a method (startEngine
).
Creating Objects
Once you’ve defined a class, you can create objects based on that class, each with its own unique set of properties.
Example: Creating Objects
$car1 = new Car();
$car1->brand = "Toyota";
$car1->model = "Camry";
$car2 = new Car();
$car2->brand = "Ford";
$car2->model = "Mustang";
Now, we have two Car
objects, $car1
and $car2
, each with its own brand and model.
Encapsulation
Encapsulation is a fundamental OOP concept that involves bundling data (attributes) and methods that operate on that data into a single unit, called a class. It provides data protection and reduces the risk of unintended interference from external code.
Example: Encapsulation
class Car {
private $brand;
private $model;
public function setBrand($brand) {
$this->brand = $brand;
}
public function getBrand() {
return $this->brand;
}
}
In this updated example, we’ve made the brand
and model
properties private and provided methods to set and get the brand
value. This ensures controlled access to the data.
Inheritance
Inheritance allows one class to inherit the properties and methods of another class. It promotes code reusability and supports the creation of hierarchies in your code.
Example: Inheritance
class ElectricCar extends Car {
public function startEngine() {
echo "The $this->brand $this->model's electric motor is running.";
}
}
Here, we’ve created a ElectricCar
class that inherits from the Car
class and overrides the startEngine
method to provide a different implementation.
Polymorphism
Polymorphism is the ability to present the same interface for different data types or objects. It allows you to use objects of different classes in a uniform way.
Example: Polymorphism
function drive($vehicle) {
$vehicle->startEngine();
}
$car = new Car();
$electricCar = new ElectricCar();
drive($car); // Outputs: "The Toyota Camry's engine is running."
drive($electricCar); // Outputs: "The Ford Mustang's electric motor is running."
In this example, the drive
function can accept both Car
and ElectricCar
objects, demonstrating polymorphism.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.