For example:
1 $abc = new MyObj();
2 $abc = new MyAnotherObj();
I assign MyObj in the first line, and the second line will assign another object. Where does the object assigned in first line go? Is it still in memory, or somewhere else?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As soon as the second line executes, the first object will have its destructor called and will be deallocated. PHP’s GC does reference counting; when you overwrite the only reference to the
MyObjinstance, the number of references drops to zero and the GC destroys the object.Note that this would happen no matter what you assigned to
$abc— you could have assigned"foobar"or42ornullor evennew MyObj()(a new instance ofMyObj) and the old object will be destroyed.See this example on ideone.