tags:

views:

900

answers:

3

I'm a little confused by some PHP syntax I've come across. Here is an example:

$k = $this->_tbl_key;

if( $this->$k)
{
   $ret = $this->_db->updateObject( $this->_tbl, $this, $this->_tbl_key, $updateNulls );
}
else
{
    $ret = $this->_db->insertObject( $this->_tbl, $this, $this->_tbl_key );
}

My question is basically what does $this->$k mean? I figured it might mean the member variable that goes by the name of whatever is in $this->_tbl_key, but how would that work? Is it possible to add member variables to a class at run-time?

+15  A: 

It'll look up whatever the value of "k" is, and treat it as a variable name. These two samples are the same:

echo ($obj->myvar);

####

$k = "myvar";
echo ($obj->$k);
John Millikin
OK, that's what I thought to begin with - I need to find where that variable is coming from, then.
Steven Oxley
+4  A: 

I believe that is a case of variable variables.

Paolo Bergantino
A: 

no it's not