Home > Backend Development > PHP Tutorial > A simple summary of object-oriented php $this $parent self_PHP tutorial

A simple summary of object-oriented php $this $parent self_PHP tutorial

WBOY
Release: 2016-07-13 10:11:48
Original
803 people have browsed it

A simple summary of object-oriented php $this $parent self

Although I have been exposed to PHP for a long time, sometimes I still feel uncertain and confused when using some basic things. Object-oriented involves a lot. Let’s briefly summarize the usage scenarios of PHP’s attributes, objects, and access methods $this $parent self.
1. PHP class attribute definition and access method:
1
2 class testClass {
3 const tConst = 1;
4 public $tVar = 2; //or public $tVar; needs to be preceded by variable modifier
5 static $tSta = 3;
6
7 public function __construct(){
8 echo $this->tConst; //No error, no output
9 echo self::tConst; //Correct output 1
10
11 echo $this->tVar; // Note that there cannot be a $ sign before tVar
12 echo self::$tVar; // Access to undeclared static property: testClass::$tVar
13 echo self::tVar; // The $ sign needs to be added before tVar, Undefined class constant 'tVar'
14
15 echo $this->tSta; //No error, no output
16 echo self::$tSta; //Correct output 3
17
18 }
19}
20
21 $otestClass = new testClass();
To summarize a few points:
When instantiating an object $otestClass = new testClass(); The () in testClass() can be omitted. When the constructor explicitly declares that parameters are required, the parameters need to be passed in here
If the referenced variable or method is declared as const (definition of constant) or static (declaration of static), then the operator must be used::
If the referenced variable or method is not declared const or static, then the operator must be used ->
To access const or static variables or methods from within a class, use self-referenced self
To access variables or methods that are not const or static from within the class, use self-referential $this
2. Where does $this-> point to?
1 class testClass {
2 var $nick = '';
3
4 public function __construct($nick){
5 $this->nick = $nick;
6 }
7
8 public function display(){
9 echo $this->nick;
10 }
11 }
12
13 $otestClass1 = new testClass('frank');
14 $otestClass1->display(); //echo $otestClass1->nick; The same result as this
In the above example, $this->display(); is actually $otestClass1->nick, so where $this points to is determined by the instantiated object, which is a pointer to the current object instance. This includes variables and methods
$this->Method() example:
1 class baseClass{
2 public function testFunc(){
3 echo "n" . 'I`m boss';
4 }
5}
6
7 class parentClass extends baseClass{
8 public function testFunc(){
9 echo "n" . 'I`m the up';
10 }
11 }
12
13 class testClass extends parentClass{
14 var $nick = '';
15
16 public function __construct($nick){
17 $this->nick = $nick;
18 }
19
20 public function display(){
21 echo $this->nick;
22 $this->testFunc();
23 }
24}
25
26 $otestClass1 = new testClass('frank');
27 $otestClass1->display();
What is the final output of this code? The key is to look at the testFunc() method.
If you use $this in a class to call a method or variable that does not exist in the current class, it will search for it in the parent class until it cannot find it and then report an error
Based on the first one, if the required method or variable is found, the search will stop. If there is the same one in its superior parent class, the currently found one will be selected
So the output of the above code is:
frank
I`m the up
3. About parent::
parent is a reference to the parent class
1
2 class A {
3 public $a = 'aa';
4 public function callFunc(){
5 echo 'parent';
6 }
7 }
8
9 class B extends A{
10 public function __construct(){
11 parent::callFunc();
12 echo "n" . $this->a;
13 }
14}
15
16 $oB = new B;
For example, in the above code, in class B, if you want to call the callFunc() method in its parent class A, you need to use parent::callFunc(), but this method in class A must be public modified , otherwise it will prompt Fatal error: Call to private method A::callfunc() from context 'B'...
Another thing to note is that for the attribute $a in the parent class, use $this when calling, but it cannot be accessed using parent. If $a is defined like this in class A: public static $a, parent::$a
is required when accessing
Note that parent is not transitive, it can only represent the parent class of the current class. If in this example, class A inherits the base class and defines the baseFunc() method in the base class, then use parent::baseFunc in class B. () is wrong and can only be used in class A.
4. Where does self:: point to?
To put it simply, self has nothing to do with a specific instance. It only points to the current class and will not be affected by a specific class object
1 class testClass{
2 public static $count = 0;
3 public $num = 0;
4
5 function __construct(){
6 self::$count++;
7 $this->num++;
8 }
9
10 function display(){
11 echo self::$count . "n";
12 echo $this->num . "n";
13 }
14}
15
16 $oTest1 = new testClass;
17 $oTest1->display(); //Output: 1 1
18
19 $oTest2 = new testClass;
20 $oTest2->display(); //Output: 2 1
In the above example, self::$cout always points to the class itself, while $num exists separately in each specific instance.
5. Summary:
$this points to the current instance
$parent is a reference to the parent class
Self is a reference to the current class itself

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/926456.htmlTechArticleA simple summary of object-oriented php $this $parent self Although I have been exposed to php for a long time, I sometimes use some basics There will still be some uncertainty and confusion when it comes to things. Object-oriented...
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