I am reading some PHP code that I could not understand:
class foo { function select($p1, $dbh=null) { if ( is_null($dbh) ) $dbh = $this->dbh ; return; } function get() { return $this->dbh; } }
I can't find $this->dbh ($dbh)
declaration from the class. My questions are:
What is the value of $this->dbh
?
Is it a local variable for function select()
?
Does $this
belong class foo
's data member? Why is there no declaration for $dbh
in this class?
$this – a pseudo-variable: Unlike other reserved keywords used in the context of class like the static, parent, etc does not need to be declared with the dollar sign ('$'). This is because in PHP $this is treated as a pseudo-variable.
The increment_counter() function will increment the value of the static variable by 1. The self keyword is used in the script to read and increment the value of the static variable. echo $object->increment_counter()."
The object operator, -> , is used in object scope to access methods and properties of an object. It's meaning is to say that what is on the right of the operator is a member of the object instantiated into the variable on the left side of the operator.
PHP $ and $$ Variables. The $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc. The $$var (double dollar) is a reference variable that stores the value of the $variable inside it.
PHP is not strict for declaration. $this->dbh is a class member. I did the following code to understand the concept:
class foo { function foo(){ $this->dbh = "initial value"; } function select($p1, $dbh=null) { if ( is_null($dbh) ) $dbh = $this->dbh ; return; } function get() { return $this->dbh; } }
It is same as:
class foo { var $dbh = "initial value"; function select($p1, $dbh=null) { if ( is_null($dbh) ) $dbh = $this->dbh ; return; } function get() { return $this->dbh; } }
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