PHP LogoObject-Oriented Programming (OOP: Classes, Objects)

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which can contain data and code. The data is in the form of fields (often called attributes or properties), and the code is in the form of procedures (often called methods).

The primary goal of OOP is to increase the flexibility and maintainability of programs. It structures software into reusable blueprints (classes) to create individual instances (objects) that interact with each other.

Key concepts in OOP are:

1. Classes:
* A class is a blueprint or a template for creating objects. It defines the common properties (attributes) and behaviors (methods) that all objects of a certain type will have.
* It's not a real entity itself but rather a definition of what an object should look like and what it can do.
* Think of a class like a cookie cutter: it defines the shape and characteristics of all cookies that will be made from it.

* Properties (Attributes/Fields): These are variables defined within a class that hold data. They represent the state of an object. For example, a `Car` class might have properties like `make`, `model`, `year`, and `color`.
* Methods (Functions): These are functions defined within a class that define the behaviors or actions an object can perform. They operate on the object's properties. For example, a `Car` class might have methods like `startEngine()`, `stopEngine()`, or `getDetails()`.

2. Objects:
* An object is an instance of a class. When you create an object from a class, you are essentially creating a real-world entity based on that blueprint.
* Each object has its own unique set of data (values for its properties), but it shares the methods defined by its class.
* Think of an object like an actual cookie made from the cookie cutter: each cookie is a separate entity, but they all share the same shape and characteristics defined by the cutter.
* Objects are created using the `new` keyword in PHP.

OOP offers several advantages:
* Modularity: Code is organized into self-contained units (objects), making it easier to understand, manage, and debug.
* Reusability: Classes can be reused to create multiple objects, and through inheritance, new classes can extend existing ones, reducing code duplication.
* Maintainability: Changes in one part of the code are less likely to affect other parts, as objects are independent.
* Scalability: OOP principles make it easier to add new features or expand the application without major refactoring.

Example Code

<?php

// Define a Class: Car
class Car {
    // Properties (Attributes)
    public $make;
    public $model;
    public $year;
    public $color;
    private $isRunning = false; // Private property to demonstrate encapsulation

    // Constructor method: Called automatically when a new object is created
    public function __construct($make, $model, $year, $color) {
        $this->make = $make;
        $this->model = $model;
        $this->year = $year;
        $this->color = $color;
        echo "A new {$this->color} {$this->make} {$this->model} ({$this->year}) has been created.<br>";
    }

    // Methods (Behaviors)
    public function startEngine() {
        if (!$this->isRunning) {
            $this->isRunning = true;
            echo "The {$this->make} {$this->model}'s engine started.<br>";
        } else {
            echo "The {$this->make} {$this->model}'s engine is already running.<br>";
        }
    }

    public function stopEngine() {
        if ($this->isRunning) {
            $this->isRunning = false;
            echo "The {$this->make} {$this->model}'s engine stopped.<br>";
        } else {
            echo "The {$this->make} {$this->model}'s engine is already off.<br>";
        }
    }

    public function getDetails() {
        $status = $this->isRunning ? "running" : "off";
        return "This car is a {$this->year} {$this->make} {$this->model} in {$this->color} color. Its engine is currently {$status}.<br>";
    }

    // Destructor method: Called automatically when the object is destroyed
    public function __destruct() {
        echo "The {$this->make} {$this->model} object has been destroyed.<br>";
    }
}

// --- Creating and Interacting with Objects ---

echo "<h2>Creating Car Objects:</h2>";
$car1 = new Car("Toyota", "Camry", 2020, "Blue");
$car2 = new Car("Honda", "Civic", 2022, "Red");

echo "<h2>Interacting with Car1:</h2>";
// Access properties and call methods on car1
echo $car1->getDetails();
$car1->startEngine();
echo $car1->getDetails();
$car1->startEngine(); // Try starting again
$car1->stopEngine();
echo $car1->getDetails();

echo "<h2>Interacting with Car2:</h2>";
// Access properties and call methods on car2
echo $car2->getDetails();
$car2->startEngine();
echo $car2->getDetails();

// Demonstrating object destruction (PHP automatically handles this at script end,
// but we can explicitly nullify an object to trigger __destruct earlier)
echo "<h2>Destroying an object:</h2>";
unset($car1); // Explicitly destroy car1, triggering __destruct
// At this point, $car1 no longer exists.

echo "<p>Script finished. All remaining objects will be destroyed automatically.</p>";

?>