Sobes.tech
Junior

Tell me about the principles of object-oriented programming (OOP).

sobes.tech AI

Answer from AI

OOP is based on four key principles:

  1. Encapsulation: Binding data (properties) and methods (functions) that work with this data into a single unit — an object. This hides implementation details and provides controlled access to data.

    <?php
    class BankAccount {
        private float $balance = 0; // Data is hidden
    
        public function deposit(float $amount): void { // Methods provide access
            if ($amount > 0) {
                $this->balance += $amount;
            }
        }
    
        public function getBalance(): float {
            return $this->balance;
        }
    }
    ?>
    
  2. Abstraction: Highlighting essential characteristics of an object and hiding non-essential details. It represents a simplified model of the real world.

    <?php
    abstract class Shape { // Abstract class
        abstract public function area(): float; // Abstract method
    
        // Can have concrete methods as well:
        public function getDescription(): string {
            return "This is a shape.";
        }
    }
    
    class Circle extends Shape {
        private float $radius;
    
        public function __construct(float $radius) {
            $this->radius = $radius;
        }
    
        public function area(): float { // Implementation of abstract method
            return M_PI * $this->radius * $this->radius;
        }
    }
    ?>
    
  3. Inheritance: Creating a new class (child) based on an existing class (parent), inheriting its properties and methods. This promotes code reuse.

    <?php
    class Animal {
        protected string $name;
    
        public function __construct(string $name) {
            $this->name = $name;
        }
    
        public function eat(): void {
            echo "{$this->name} is eating.\n";
        }
    }
    
    class Dog extends Animal { // Dog inherits from Animal
        public function bark(): void {
            echo "{$this->name} says Woof!\n";
        }
    }
    
    $dog = new Dog("Buddy");
    $dog->eat();  // Using inherited method
    $dog->bark();
    ?>
    
  4. Polymorphism: The ability of objects of different classes to respond to the same method call in their own way. "Many forms".

    <?php
    interface Speakable { // Interface for polymorphism example
        public function speak(): void;
    }
    
    class Cat implements Speakable {
        public function speak(): void {
            echo "Meow!\n";
        }
    }
    
    class Duck implements Speakable {
        public function speak(): void {
            echo "Quack!\n";
        }
    }
    
    function makeSound(Speakable $animal): void { // Function accepts different object types
        $animal->speak(); // Call the same method
    }
    
    $cat = new Cat();
    $duck = new Duck();
    
    makeSound($cat); // Calls Cat::speak()
    makeSound($duck); // Calls Duck::speak()
    ?>