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?
That dollar sign means: we're in the system shell, i.e the program that you're put into as soon as you open the Terminal app. The dollar sign is often the symbol used to signify where you can begin typing in commands (you should see a blinking cursor there).
$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).
The @ symbol is the error control operator ("silence" or "shut-up" operator). It makes PHP suppress any error messages (notice, warning, fatal, etc) generated by the associated expression. It works just like a unary operator, for example, it has a precedence and associativity.
It means you pass a reference to the string into the method. All changes done to the string within the method will be reflected also outside that method in your code.
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);
I believe that is a case of variable variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With