No, because $object1 in class1:doSomething() is undefined. If you use $object there instead, it will work, because doSomething() will call $object->setAVariable(), what is in this case the same object, like $object2 fromt the outer scope (you gave that object to doSomethig()).