如果PHP总是通过引用复制对象,那么如果在方法中创建的对象被分配给成员变量会发生什么?

I'm confused what would happen in this situation:

public function foo() {
    $obj = new \stdClass();
    $obj->bar = 'foobar';

    $this->obj = $obj;
}

If $obj is copied by reference, then when foo() returns, won't $obj be deleted and thus $this->obj point to an object that no longer exists?

Inside the method this is what it looks like:

$obj ---------.
               >-- [OBJECT]
$this->obj --´

When the method returns the $obj variable is destroyed, and the $this->obj variable will still point to the object:

$this->obj ------> [OBJECT]

The [OBJECT] value will only disappear (or be garbage collected) once all references to it have been removed.