๐ ๐จ๐ฎ๐ง๐๐๐ญ๐ข๐จ๐ง๐ฌ ๐จ๐ ๐๐๐ฃ๐๐๐ญ-๐๐ซ๐ข๐๐ง๐ญ๐๐ ๐๐ซ๐จ๐ ๐ซ๐๐ฆ๐ฆ๐ข๐ง๐
๐๐ง๐๐๐ฉ๐ฌ๐ฎ๐ฅ๐๐ญ๐ข๐จ๐ง, ๐๐๐ฌ๐ญ๐ซ๐๐๐ญ๐ข๐จ๐ง, ๐๐ง๐ก๐๐ซ๐ข๐ญ๐๐ง๐๐, ๐๐ง๐ ๐๐จ๐ฅ๐ฒ๐ฆ๐จ๐ซ๐ฉ๐ก๐ข๐ฌ๐ฆ
1. Encapsulation:
Encapsulation is the technique of grouping the data and the operations that manipulate the data within a single unit (class). This hides the internal state of the object and only exposes necessary functionality.
Benefits:
- Data Hiding: Protects the objectโs state from unauthorized access.
- Modularity: Changes in one part of the code can be made independently of other parts.
- Ease of Maintenance: Reduces complexity and increases code readability.
Example: A โCarโ class can encapsulate data like brand, model, and price, and methods to start the engine or stop the car. External code interacts with the car using these methods without knowing the internal workings.
class Car {
private $brand;
private $model;
private $price;
public function __construct($brand, $model, $price) {
$this->brand = $brand;
$this->model = $model;
$this->price = $price;
}
public function startEngine() {
echo "{$this->brand} {$this->model} engine started"
}
public function stopCar() {
echo "{$this->brand} {$this->model} stopped"
}
}
$myCar = new Car('Toyota', 'Corolla', '2000');
$myCar->startEngine();
$myCar->stopCar();
2. Abstraction:
Abstraction involves reducing complexity by hiding unnecessary details and showing only essential features to the user. This is achieved by defining clear interfaces and hiding internal implementation.
Benefits:
- Simplifies Complex Systems: By exposing only high-level mechanisms.
- Reduces Impact of Change: Changes to the implementation donโt affect users of the abstraction.
- Promotes Reusability: Common interfaces can be reused across different parts of the application.
Example: When using a remote control, the user doesnโt need to know about the internal electronics. They just use the buttons to control the device.
abstract class RemoteControl {
abstract public function powerOn();
abstract public function powerOff();
}
class TVRemoteControl extends RemoteControl {
public function powerOn() {
echo "TV is now ON.";
}
public function powerOff() {
echo "TV is now OFF.";
}
}
$myRemote = new TVRemoteControl();
$myRemote->powerOn();
$myRemote->powerOff();
3. Inheritance:
Inheritance allows a class (derived or subclass) to inherit properties and behaviors from another class (base or parent). This promotes code reuse and hierarchical class relationships.
Benefits:
- Code Reusability: Common functionality can be written once in the base class.
- Extensibility: New features can be added to existing classes without modifying them.
- Class Hierarchies: Logical structures can be created to represent relationships.
Example: A โVehicleโ class can be the base class for subclasses like โCarโ, โPlaneโ, and โBoatโ. The subclasses inherit characteristics from the โVehicleโ class but can also add their own specific behaviors.
class Vehicle {
protected $brand;
protected $model;
public function _construct($brand, $model) {
$this->brand = $brand;
$this->model = $model;
}
public function move() {
echo "The {$this->brand} {$this->model} is moving.";
}
class Car extends Vehicle {
private $price;
public function _construct($brand, $model, $price) {
parent::_construct($brand, $model);
$this->price = $price;
}
public function startEngine() {
echo "The {$this->brand} {$this->model}'s engine is started." }
}
class Boat extends Vehicle {
public function sail() {
echo "The {$this->brand} {$this->model} is sailing.";
}
}
$myCar = new car('Toyota', 'Corolla', 20000);
$myCar->move();
$myCar->startEngine();
$myBoat = new Boat('Yamaha', 'FX Cruiser');
$myBoat->move();
$myBoat->sail();
4. Polymorphism:
Polymorphism is the ability of an object to take many forms and behave differently. It allows objects of different classes to respond to the same method call in unique ways.
Benefits:
- Flexibility: Allows for the use of objects interchangeably.
- Extensibility: New functionality can be easily integrated with minimal changes.
- Maintainability: Reduces code complexity by using a unified interface.
Example: If you have an โAnimalโ class with a โmakeSound()โ method, and subclasses like โDogโ and โCatโ each overriding โmakeSound()โ, calling โmakeSound()โ on a โDogโ object will bark, while a โCatโ object will meow.
abstract class Animal {
abstract public function makeSound();
}
class Dog extends Animal {
public function makeSound() {
return "Woof!";
}
}
class Cat extends Animal {
public function makeSound() {
return "Meow!";
}
}
function animalSound(Animal $animal) {
echo $animal->makeSound();
}
$myDog = new Dog();
$myCat = new Cat();
animalSound($myDog);
animalSound($myCat);