What is the purpose of encapsulating a method in PHP?

Encapsulating a method in PHP helps to organize code by grouping related functionality together and hiding the implementation details from the outside world. This also promotes code reusability and makes it easier to maintain and update the code in the future. By encapsulating methods, you can control access to the functionality and prevent unintended modifications.

class MyClass {
    private $data;

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

    private function processData() {
        // Implementation details
    }

    public function getData() {
        $this->processData();
        return $this->data;
    }
}

$instance = new MyClass('example data');
echo $instance->getData();