Analysis of this keyword usage in php

Source: Internet
Author: User
This article mainly introduces the usage of the this keyword in php, and analyzes the principles and usage skills of internal variables and methods of this keyword using specific examples, if you need this keyword, refer to the example in this article to describe the usage of this keyword in php. We will share this with you for your reference. The details are as follows:

The following defines a Cart class

 Items [$ artnr] + = $ num;} // retrieves $ num from the car. function remove_item ($ artnr, $ num) {if ($ this-> items [$ artnr]> $ num) {$ this-> items [$ artnr]-= $ num; return true ;} else {return false ;}}}?>

You can use a piece of code to describe the problem. within the definition of a class, you cannot know which name objects are accessible: When writing a Cart class, the object name will be named $ cart or $ another_cart. Therefore, you cannot use $ cart-> items in the class. However, you can use the pseudo variable $ this to access your own functions and variables within the class definition. $ This variable can be understood as "my own" or "current object ". Therefore, '$ this-> items [$ artnr] + = $ num' can be interpreted as "adding $ num to the $ artnr counter of my own item array" or "in the current object's the $ artnr counter of the item array plus $ num ".

The above is the content of this keyword usage analysis in php. For more articles, please follow the PHP Chinese website (www.php1.cn )!

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: info-contact@alibabacloud.com 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.