1

If I were to do this:

$bob = new Bob();

function AnnoyBob( Bob $bob )
{
    $bob = NULL;
} // < A 

AnnoyBob( $bob );

A: Does $bob now lose its reference to the original pointer and now point to NULL thus having to get collected on the next GC?

$bob = new Bob();

function AnnoyBob( Bob $bob )
{
    unset( $bob );
} // < B    

AnnoyBob( $bob );

B: Is the $bob pointer now instantly releasing the memory for overwriting?

C: What if you were to pass in the reference here instead: ..( Bob &$bob )

enter image description here

My point being, I see people nullifying pointers not realising that they are now leaving GC to do their dirty work... whereas if you use unset, I am wondering if it explicitly marks it to be available for overwriting then and there (deleted)!

1 Answer 1

4

You will only ever modify the variable inside the function. Your Bob instance will continue to exist, because variables outside the function are still referencing it. Bob won't get garbage collected until all references to it are gone/have become inaccessible.

An object instance exists somewhere in a pool of object instances in memory. A variable "holding an object" actually just holds an identifier of that object in memory. If you unset or overwrite that reference, the object itself is not altered. When passing it into a function, you're just passing a copy of the identifier into the function; then see previous sentence.

If you're passing it by reference, then you can modify the caller's variable's content. If you just unset this variable, it will just unset it inside the function, so nothing much happens to Bob. Only if you pass it by reference and you reassign another value to that variable will Bob get garbage collected.

I.e., only in this scenario are you overwriting the last object reference and cause it to become garbage collected:

$bob = new Bob();

function AnnoyBob(Bob &$bob) {
    $bob = NULL;
}

AnnoyBob($bob);
Sign up to request clarification or add additional context in comments.

14 Comments

I've added to my OP, My question was most likely a request for vindication on not nullifying pointers... rather unsetting them. I guess from your last part I am indeed right.
Only thing I'd add to this, assuming I'm remembering correctly, is that PHP garbage collects "dead" variables at function closure - so there's not much point in unsetting $bob inside the function just before you close it.
@CD001 A non-existent variable will will cause warnings to be emitted, and its value will be substituted by null; however a set variable holding the value null is a perfectly legitimate variable with a value. There are differences.
@Jimmy That's what I'm saying. PHP does not give you any programmatic access to memory. At all. It isolates you entirely from it. PHP doesn't have anything resembling C pointers. The only thing you can do is to force gc to run. But PHP will run gc as needed, i.e. when you're approaching the memory limit or every so often for long running programs.
@Jimmy You're welcome. Worrying about memory (beyond generalities) is a waste of time in PHP. Unsetting or nullifying variables doesn't really do anything for the most part. You should write a lot of short self-contained functions through which variables go out of scope quickly automatically; that's the best memory control you can do because it's implicit, you don't need to waste a lot of code on it and it makes your code better overall.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.