PHP keyword This points to the current object pointer _php tutorial

Source: Internet
Author: User

  1. < ? PHP
  2. Class UserName
  3. {
  4. Defining properties
  5. Private $name;
  6. Defining constructors
  7. function __construct ($name)
  8. {
  9. $this- > name = $name;
    The this pointer is already used here
  10. }
  11. Destructors
  12. function __destruct () {}
  13. Print User name member function
  14. function Printname ()
  15. {
  16. Print ($this->name);
    and using the PHP keyword this pointer
  17. }
  18. }
  19. Instantiating an Object
  20. $ Nameobject = New UserName
    ("Heiyeluren");
  21. Perform printing
  22. $nameObject- > Printname ();
    Output: Heiyeluren
  23. Second instantiation of the object
  24. $ NameObject2 = New UserName ("PHP5");
  25. Perform printing
  26. $nameObject 2- > printname ();//output: PHP5
  27. ?>

Let's see, the class above uses the this pointer in 11 rows and 20 rows, so who is this at that point? In fact this is at the time of instantiation to determine who to point to, such as when the first instantiation of the object (25 rows), then this is pointing to the $nameobject object, then the execution of 18 lines of printing when the print ($this , name), Then of course the output is "Heiyeluren".

In the second instance, print ($this->name) becomes print ($nameObject 2->name), so it outputs "PHP5". So the PHP keyword this is a pointer to the current object instance and does not point to any other object or class.


http://www.bkjia.com/PHPjc/445969.html www.bkjia.com true http://www.bkjia.com/PHPjc/445969.html techarticle php classusername {//define attribute Private$name;//define constructor Function__construct ($name) {$this-name = $name;//Use this pointer} destructor Function_ ...

  • 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.