This keyword usage analysis _php techniques in PHP

Source: Internet
Author: User

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.

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.