What are the benefits of using method chaining in PHP programming?

Method chaining in PHP programming allows you to call multiple methods on the same object in a single line of code, which can lead to more concise and readable code. This can help improve code maintainability and reduce the number of temporary variables needed. Additionally, method chaining can help streamline the process of invoking multiple methods on the same object.

class Example {
    private $value;

    public function setValue($value) {
        $this->value = $value;
        return $this; // Return $this to enable method chaining
    }

    public function addValue($amount) {
        $this->value += $amount;
        return $this; // Return $this to enable method chaining
    }
}

$example = new Example();
$result = $example->setValue(5)->addValue(10)->setValue(20)->addValue(5)->setValue(30)->addValue(15)->value;
echo $result; // Output: 50