This article mainly introduces the use of this keyword in PHP, combined with a specific example of this keyword access to the internal variables and methods of the principle and related usage skills, the need for friends can refer to the next
This example describes the use of this keyword in PHP. Share to everyone for your reference, as follows:
The following defines a cart class
<?phpclass cart{ var $items;//items in the shopping cart //Put $num $artnr in the car function Add_item ($ARTNR, $num) { $this->items[$artnr] + = $num; } Take $num $artnr out of the car function Remove_item ($ARTNR, $num) { if ($this->items[$artnr] > $num) { $ this->items[$artnr]-= $num; return true; } else { return false;}} ? >
To illustrate the problem in a piece of code, within the definition of a class, you cannot tell what name the object is accessible: When you write the Cart class, you do not know that the name of the object will be named $cart or $another _cart. Thus you cannot use the $cart->items in the class. However, for a class-defined internal access to its own functions and variables, pseudo-variable $this can be used to achieve this purpose. $this variable can be interpreted as "my own" or "current object". Thus ' $this->>items[$artnr] + = $num ' can be understood as "the $ARTNR counter of my own array of items plus $num" or "$artnr counter in the current object's array of items $num".
The above is the whole content of this article, I hope that everyone's study has helped.