This keyword usage analysis in PHP

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.