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
- class self_test {
- static $instance;
- public function __construct() {
- Self :: $instance = ' instance '; Static properties can only be accessed through self
- }
- public function Tank() {
- return self :: $instance; Accessing static properties
- }
- }
- $STR = new Self_test ();
- echo $str->tank ();
Page Output: Instance
- class self_test {
- static $instance;
- public function __construct() {
- Self :: $instance = ' dell ';
- }
- static public function Pentium() {
- return self :: $instance; Static methods can also continue to access static variables, which need to be added $
- }
- public function Tank() {
- return self ::p entium (); Accessing static methods Pentium ()
- }
- }
- $STR = new Self_test ();
- echo $str->tank ();
Page Output: Dell
2.self can access const-defined constants
- class self_test {
- Const NAME = ' tancy ';
- public function Tank() {
- return self::name;
- }
- }
- $STR = new Self_test ();
- 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
- class self_test {
- Public $public;
- private $private;
- protected $protected;
- public function __construct() {
- $this->public = ' public ';
- $this->private = ' private ';
- $this->protected = ' protected ';
- }
- public function Tank() {
- return $this->public;
- }
- public function Dell() {
- return $this->private;
- }
- public function datesrt() {
- return $this->protected;
- }
- }
- $STR = new Self_test ();
- echo $str->tank ();
- echo "</br>";
- echo $str->dell ();
- echo "</br>";
- 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