PHP - What is OOP?

OOP (Object-Oriented Programming) in PHP is a programming paradigm that uses objects and classes to organize and structure code. It's a way of designing software by modeling real-world entities as objects that have properties (variables) and methods (functions).

Key Concepts of OOP in PHP:

  1. Class
    A blueprint for creating objects. It defines the structure and behavior of the object.

    class Car { public $color;
    public function drive() { echo "Driving..."; } }
  2. Object
    An instance of a class.

    $myCar = new Car();
    $myCar->drive();
  3. Encapsulation
    Hides the internal state of an object and only allows access through public methods.

    class BankAccount { private $balance = 0;
    public function deposit($amount) { $this->balance += $amount; } }
  4. Inheritance
    Allows one class to inherit properties and methods from another.

    class Vehicle
    { public function
    start()
    { echo "Vehicle started"; } }
    class Car extends Vehicle
    { public function honk()
    { echo "Beep!"; } }
  5. Polymorphism
    Allows objects of different classes to be treated as objects of a common parent class.

    class Animal
    { public function speak()
    { echo "Animal sound"; } }
    class Dog extends Animal
    { public function speak() { echo "Bark"; } }
  6. Abstraction
    Hides complex implementation details and shows only essential features.

    abstract class Shape
    { abstract public function area(); }
    class Circle extends Shape { public function area()
    { return 3.14 * 5 * 5; } }

OOP helps in creating reusable, scalable, and maintainable code.