How can objects created within a method of a class in PHP be accessed outside of the method's scope?

Objects created within a method of a class in PHP can be accessed outside of the method's scope by assigning the created object to a class property. This way, the object becomes accessible to other methods within the class.

class MyClass {
    private $myObject;

    public function createObject() {
        $this->myObject = new MyObject();
    }

    public function useObject() {
        // Access the object created in createObject method
        $this->myObject->doSomething();
    }
}

$myClass = new MyClass();
$myClass->createObject();
$myClass->useObject();