How does object-oriented programming (OOP) in PHP differ from procedural programming?

Object-oriented programming (OOP) in PHP differs from procedural programming by focusing on creating objects that contain both data and methods to operate on that data, promoting code reusability and organization. In OOP, classes are used to define objects, which encapsulate data and behavior, while in procedural programming, functions operate on data directly. OOP allows for better code organization, modularity, and maintenance compared to procedural programming.

// Example of a simple class in PHP
class Car {
    public $brand;
    public $model;

    public function __construct($brand, $model) {
        $this->brand = $brand;
        $this->model = $model;
    }

    public function getDetails() {
        return $this->brand . ' ' . $this->model;
    }
}

// Creating an instance of the Car class
$myCar = new Car('Toyota', 'Corolla');

// Accessing object properties and methods
echo $myCar->getDetails(); // Output: Toyota Corolla