HELP!!! I have hit a brick wall in figuring out something with passing objects by reference in PHP5. Is what I want to do possible???😕

I have a situation where I want to:

1) pass an object into a function
2) have that object unset in the function
3) have this unset which occurs inside the function propogate up one level of scope to where the function call was made.

I am guessing when I unset($obj) inside the function that I am only unsetting the reference to $obj whose scope is limited to the life of that function call...

In the example code shown below, I would like the following function call:

[code=php]unsetObj($test);[/code]

actually unset the object "$test". In my test code shown below, "$test" does not get unset in the scope of main.:mad:

Background: I have a mapper class which acts as the interface between the PHP program and the database, one of the methods inside this mapper is delete. Upon a successful delete from the database, I would also like the object which we just deleted from the database to be unset by the delete method and have this unset propagate to the calling PHP code so the PHP code and database stay in synch.

Thanks in advance for any help anyone may be able to provide!!!🙂

-james

Example output of test code shown below if viewed in a browser:

unsetting object inside unsetObj
succeeded in unseting object in scope of unsetObj

failed to unset object in scope of main

test code for viewing in a browser (for viewing from command line, replace <br> with \n in the echos)

class test {
}
function unsetObj($obj){
	echo "unsetting object inside unsetObj<br>";
	unset($obj);
	if (isset($obj)){
		echo "failed to unset object in scope of unsetObj<br>";
	} else {
		echo "succeeded in unseting object in scope of unsetObj<br>";
	}
}


// make a new test object
$test = new test();

// attempt to unset our test object
unsetObj($test);

// check to set if it was really unset
if (isset($test)){
	echo "failed to unset object in scope of main<br>";
} else {
	echo "succeeded in unseting object in scope of main<br>";
}

    One workaround is to pass the object by reference and then set it to null, e.g.,

    function unsetObj(&$obj){
        echo "unsetting object inside unsetObj<br>";
        $obj = null;
        if (isset($obj)){
            echo "failed to unset object in scope of unsetObj<br>";
        } else {
            echo "succeeded in unseting object in scope of unsetObj<br>";
        }
    }

    Note that you actually have to pass it by reference otherwise all you will do is set the reference (as in pointer) to the object to null.

      THANKYOU!! 🙂

      BTW, do you know what is the difference between unset($obj) and $obj=null in the context of objects? I am confused as to why pass by reference and unset($obj) does not work.

      Thanks Again!!

      -james

        The unset() does not work because, "When you unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed," (from http://php.net/manual/en/language.references.unset.php).

        By setting it to null, you are then actually overwriting the variable content (which was an object) with a new value (a NULL type). For most practical purposes, a null-value variable is equivalent to an undefined variable.

          BTW, do you know what is the difference between unset($obj) and $obj=null in the context of objects?

          The difference is that if you try and access (read from) a variable is that is unset, a notice will be generated since the variable no longer exists. However, you can freely access a variable that has been set to null, except that it is not an object (in terms of OOP). On the other hand, if you are performing a check with isset, there is no difference.

          I am confused as to why pass by reference and unset($obj) does not work.

          I do not know exactly why does it not work, but I do know that it is probably intentional since it is documented in the PHP manual's entry on [man]unset[/man].

            I'm just guessing here, but since unset() is also technically a function, you would need to pass by reference there as well.

            ie -

            unset(&$obj)

            But I'm guessing the unset() function will not accept a reference (...maybe?).


            EDIT: Never mind...the experts have already spoken. heh

              ixalmida wrote:

              I'm just guessing here, but since unset() is also technically a function, you would need to pass by reference there as well.

              According to the PHP manual, unset() is a language construct, not a function, though that probably does not matter here. Call time pass by reference has been deprecated for some time, and should make no difference anyway, according to the PHP manual entry that NogDog linked to.

                Thank You laserlight and NogDog for your quick and complete responses 🙂

                I was banging my head against the wall for awhile trying to get this to work...

                  unset() is a language construct

                  I get that. Similar in form anyway, if not function.

                  I don't really think in terms of OOP. I may eventually make the paradigm-shift if the need arises.

                    Write a Reply...