What are the best practices for handling data transfer between objects in PHP, specifically in scenarios where one object extends another?

When transferring data between objects in PHP, especially when one object extends another, it is best practice to use getter and setter methods to access and modify the data. This ensures encapsulation and allows for better control over the data being transferred. By using getter and setter methods, you can maintain the integrity of the data and prevent direct access to object properties.

<?php

class ParentClass {
    protected $data;

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

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

class ChildClass extends ParentClass {
    public function transferData($data) {
        $this->setData($data);
    }
}

// Example usage
$parent = new ParentClass();
$child = new ChildClass();

$child->transferData("Hello World");
echo $parent->getData(); // Output: Hello World

?>