What are the advantages of using OOP in PHP compared to procedural programming?

Using object-oriented programming (OOP) in PHP offers several advantages over procedural programming. OOP allows for better code organization and reusability through the use of classes and objects. It also enables encapsulation, which helps in creating more secure and maintainable code. In addition, OOP promotes inheritance and polymorphism, allowing for easier extensibility and flexibility in your code.

// Example code demonstrating the use of OOP in PHP

class Animal {
    public $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function speak() {
        echo "The $this->name is speaking.";
    }
}

$dog = new Animal("Dog");
$dog->speak();