Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does using a dollar sign after $this-> in PHP mean?

Tags:

syntax

oop

php

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?

like image 274
Steven Oxley Avatar asked Oct 20 '08 05:10

Steven Oxley


People also ask

What does a dollar sign mean in code?

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).

What does '$' mean in JavaScript?

$ 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).

What is the use of this symbol in PHP?

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.

What does &$ mean in PHP?

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.


2 Answers

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);
like image 97
John Millikin Avatar answered Sep 21 '22 16:09

John Millikin


I believe that is a case of variable variables.

like image 21
Paolo Bergantino Avatar answered Sep 21 '22 16:09

Paolo Bergantino