Logo de Grรฉgory Leroux

Grรฉgory Leroux

  • ๐…๐จ๐ฎ๐ง๐๐š๐ญ๐ข๐จ๐ง๐ฌ ๐จ๐Ÿ ๐Ž๐›๐ฃ๐ž๐œ๐ญ-๐Ž๐ซ๐ข๐ž๐ง๐ญ๐ž๐ ๐๐ซ๐จ๐ ๐ซ๐š๐ฆ๐ฆ๐ข๐ง๐ 

    ๐„๐ง๐œ๐š๐ฉ๐ฌ๐ฎ๐ฅ๐š๐ญ๐ข๐จ๐ง, ๐€๐›๐ฌ๐ญ๐ซ๐š๐œ๐ญ๐ข๐จ๐ง, ๐ˆ๐ง๐ก๐ž๐ซ๐ข๐ญ๐š๐ง๐œ๐ž, ๐š๐ง๐ ๐๐จ๐ฅ๐ฒ๐ฆ๐จ๐ซ๐ฉ๐ก๐ข๐ฌ๐ฆ

    php image

    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);