Detailed explanation of the difference between self and $this in PHP

小云云
Release: 2023-03-21 11:24:02
Original
1582 people have browsed it

Object-oriented programming (OOP, Object Oriented Programming) has now become a basic skill for programmers. Using OOP ideas for advanced programming of PHP is very meaningful for improving PHP programming capabilities and planning web development architecture.

Here I am mainly talking about the difference between this and self keywords. Literally understood, it refers to this and oneself. Let me give a preliminary explanation first. This is a pointer to the current object (can be regarded as a pointer in C), and self is a pointer to the current class. We frequently use pointers to describe here.

Because I often use frameworks, I don’t know some of the underlying operating mechanisms and methods of PHP, so I like to study these underlying things recently. Although the writing is not very good, I have done it step by step on my own, and it is easier to understand. Most of the information on the Internet now is either copied from the manual and is very official, or it is the same article. It is indeed difficult to understand, so I can only do it myself. Come and get familiar with it

1. self

. 1.self can access the static properties and static methods in this class, and can access the static properties and static methods in the parent class. When using self, you don’t need to instantiate

  1. class self_test {  
        static$instance;  
        publicfunction __construct(){  
            self::$instance = 'instance';//静态属性只能通过self来访问
        }  
        publicfunction tank(){  
            return self::$instance;//访问静态属性
        }  
    }  
    $str = new self_test();  
    echo$str->tank();
    Copy after login

Page output: instance

  1. class self_test {  
        static$instance;  
        publicfunction __construct(){  
            self::$instance = 'dell';  
        }  
        staticpublicfunction pentium(){  
            return self::$instance;//静态方法也可以继续访问静态变量,访问时需要加$
        }  
        publicfunction tank(){  
            return self::pentium();//访问静态方法pentium()
        }  
    }  
    $str = new self_test();  
    echo$str->tank();
    Copy after login

Page output: dell
2.self can access constants defined by const

  1. class self_test {  
        const  NAME = 'tancy';  
        publicfunction tank(){  
            return self::NAME;  
        }  
    }  
    $str = new self_test();  
    echo$str->tank();
    Copy after login

Page output: tancy

二.this

1.this can call methods and properties in this class, and can also call adjustable methods and properties in the parent class. It can be said that except for static and const constants, basically everything else can be contacted using this

  1. class self_test {  
        public$public;  
        private$private;  
        protected$protected;  
        publicfunction __construct(){  
            $this->public = 'public';  
            $this->private = 'private';  
            $this->protected = 'protected';  
        }  
        publicfunction tank(){  
            return$this->public;  
        }  
        publicfunction dell(){  
            return$this->private;  
        }  
        publicfunction datesrt(){  
            return$this->protected;  
        }  
    }  
    $str = new self_test();  
    echo$str->tank();  
    echo"</br>";  
    echo$str->dell();  
    echo"</br>";  
    echo$str->datesrt();
    Copy after login

Page output: public
private
protected

一句话,self是引用静态类的类名,而$this是引用非静态类的实例名。
Copy after login

Related recommendations:

In-depth Detailed explanation of php self and $this_PHP tutorial

The above is the detailed content of Detailed explanation of the difference between self and $this in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!