How can objects be nested or have subobjects in PHP classes?

To nest objects or have subobjects in PHP classes, you can create properties within a class that are instances of other classes. This allows for a hierarchical structure where objects can contain or be composed of other objects. By defining these relationships within the class definitions, you can easily access and manipulate nested objects.

class Subobject {
    public $name;

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

class ParentObject {
    public $subobject;

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

// Creating an instance of ParentObject with a nested Subobject
$parent = new ParentObject('Nested Object');
echo $parent->subobject->name; // Output: Nested Object