How does the concept of abstraction and reusability play a role in OOP in PHP, particularly when designing classes and methods?

Abstraction and reusability in OOP in PHP allow for creating classes and methods that can be used in various contexts without needing to rewrite the same code. By abstracting common functionalities into classes and methods, you can easily reuse them in different parts of your application, promoting code reusability and reducing redundancy.

<?php
// Example of abstraction and reusability in PHP OOP

// Abstract class with a method that can be reused in different subclasses
abstract class Shape {
    abstract public function calculateArea();
}

// Subclass that extends the Shape class and implements the calculateArea method
class Circle extends Shape {
    private $radius;

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

    public function calculateArea() {
        return pi() * pow($this->radius, 2);
    }
}

// Subclass that extends the Shape class and implements the calculateArea method
class Rectangle extends Shape {
    private $width;
    private $height;

    public function __construct($width, $height) {
        $this->width = $width;
        $this->height = $height;
    }

    public function calculateArea() {
        return $this->width * $this->height;
    }
}

// Create instances of the Circle and Rectangle classes and calculate their areas
$circle = new Circle(5);
echo "Circle Area: " . $circle->calculateArea() . "\n";

$rectangle = new Rectangle(4, 6);
echo "Rectangle Area: " . $rectangle->calculateArea() . "\n";
?>