Self,this Differences and field operations in PHP

Source: Internet
Author: User

Object-Oriented programming (Oop,object orientedprogramming) has now become a basic skill for programmers. Using the idea of OOP for high-level programming of PHP is significant for improving PHP programming capabilities and planning the Web development architecture.

Here I am mainly talking about the difference between the this,self keywords. In the literal sense, it means this and yourself. First of all, this is a pointer to the current object (which can be seen as a pointer in C), and self is a pointer to the current class. We use pointers here frequently to describe

Because often use the framework, so the PHP bottom of some operating mechanisms and methods are not, so recently more like to study these low-level things, writing, although not very good, but they step by step in the field operation, understanding is easier, Now most of the information on the Internet is either in accordance with the manual copy of the very official words, or those of the same-like articles, it is really some bad understanding, so you can only hand to know

I. Self. 1.self can access static properties and static methods in this class, and can access static and static methods in the parent class. When using self, you can not instantiate the
  1. class self_test {
  2. static $instance;
  3. public function __construct() {
  4. Self :: $instance = ' instance '; Static properties can only be accessed through self
  5. }
  6. public function Tank() {
  7. return self :: $instance; Accessing static properties
  8. }
  9. }
  10. $STR = new Self_test ();
  11. echo $str->tank ();
Page Output: Instance
  1. class self_test {
  2. static $instance;
  3. public function __construct() {
  4. Self :: $instance = ' dell ';
  5. }
  6. static public function Pentium() {
  7. return self :: $instance; Static methods can also continue to access static variables, which need to be added $
  8. }
  9. public function Tank() {
  10. return self ::p entium (); Accessing static methods Pentium ()
  11. }
  12. }
  13. $STR = new Self_test ();
  14. echo $str->tank ();
Page Output: Dell

2.self can access const-defined constants
    1. class self_test {
    2. Const NAME = ' tancy ';
    3. public function Tank() {
    4. return self::name;
    5. }
    6. }
    7. $STR = new Self_test ();
    8. echo $str->tank ();
Page output: tancy

Two. This 1.this can call methods and properties in this class, you can also call the parent class can be adjusted methods and properties, you can say that in addition to static and const constants, basically other can use this contact
  1. class self_test {
  2. Public $public;
  3. private $private;
  4. protected $protected;
  5. public function __construct() {
  6. $this->public = ' public ';
  7. $this->private = ' private ';
  8. $this->protected = ' protected ';
  9. }
  10. public function Tank() {
  11. return $this->public;
  12. }
  13. public function Dell() {
  14. return $this->private;
  15. }
  16. public function datesrt() {
  17. return $this->protected;
  18. }
  19. }
  20. $STR = new Self_test ();
  21. echo $str->tank ();
  22. echo "</br>";
  23. echo $str->dell ();
  24. echo "</br>";
  25. echo $str->datesrt ();
Page output: Public
Private
Protected
In a word, self is a class name that refers to a static class, and $this is an instance name that references a non-static class.

Self,this Differences and field operations in PHP

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.