This example describes the use of this keyword in PHP. Share to everyone for your reference, specific as follows:
The following defines a cart class
<?php
class Cart
{
var $items;//items in the cart//
put $num $artnr into the car
function Add_item ($ARTNR, $num) c6/>{
$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;}}}
? >
With a piece of code that explains the problem, within the definition of a class, you can't know what name the object is accessible to: when you write the Cart class, the name of the object will be named $cart or $another _cart. Therefore you cannot use $cart->items in the class. However, in order to access its own functions and variables within the class definition, the pseudo variable $this can be used to achieve this goal. $this variable can be understood as "my own" or "current object." Thus the ' $this->>items[$artnr] + + $num ' can be understood as ' $artnr counter plus $num ' of the array of my own objects plus $artnr ' or ' $num counter of the objects array of the current object.
More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.