PHP - get object parent
I have开发者_如何学C a function that is called like this:
foo($object->ID);
and in the function I need to somehow select $object
if $object->ID
is passed as a variable.
function foo($id = NULL){
if($id != NULL) ... // here I want to get $object
else ...
}
How can I do this?
That is not possible. You are passing a number without any information about its origin. Do this
foo($object)
function foo($object){
if($object->ID !== null) ... // work with $object
else ... // work with ID
}
why not:
foo($object);
and
function foo($localObject){
if(isset($localObject->id)){
}
}
You need to pass the object in as an argument instead of the ID.
If I understand what you're asking correctly, why not just pass the object itself by ref?
function foo(&$obj)
{
if($obj != NULL && $obj->ID != NULL)
{
// ...process your stuff
}
}
My PHP's pretty rusty, but I'm fairly sure that's how you pass by ref...
精彩评论