Object OOP
1. Fatal error: Using $ this when not in object context
This error is certainly easy to learn from OOP, because you do not really understand it. The class's accessibility (accessible) can also be called the scope. You can also think that a Chinese is abroad and does not belong to any culture, he does not speak a foreign language (maybe he knows something), but he cannot communicate with foreigners because they are not born in a common country.
So how does an error occur? See the following example:
1 <? Php
2 class Trones {
3 static public $ fire = "I am fire .";
4 public $ water = "I am water ";
5
6 static function getFire (){
7 return $ this-> fire; // wrong
8}
9 static function getWater (){
10 return $ self: water; // wrong
11}
12
13 static function Fire (){
14 return self: $ fire; // be sure you use self to access the static property before you invoke the function
15}
16}
17
18 /*
19 Fatal error: Using $ this when not in object context
20 */
21 // echo Trones: getFire ();
22 // echo Trones: getWater ();
23
24 // correct
25 echo Trones: Fire ();
26 echo "<br/> ";
27 $ trones = new Trones;
28 $ trones-> fire; // Notice: Undefined property: Trones: $ fire (base on defferent error setting) simple is error
29 echo Trones: $ fire;
This error is classic and practical. Let's first look at the definition of static:
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can ).
When defining a class's properties or methods as static, they can be directly accessed without initializing a class. A property defined as static cannot be accessed by class objects using object operators *-> * (it can be accessed through static methods ).
Example:
Line 7 and Line 10 make the same mistake. The first one is to use object operators to access static variables. Let's take a look at the definition. $ this is a pseudo variable equivalent to an object and an instance. If you use the object operator> access, an error is returned.
Similarly, you cannot use the static OPERATOR: to access a public variable. The correct access should be 14 rows and 25 rows. One is to access (self ::== Trones: :) In the class definition, and the other is to access outside the class.
For inheritance classes, the above rules are also suitable.
------------------------------------------------------ Split line --------------------------------------------------------------------------------------
2. Fatal error: Call to private method
Recently, there was a series of games called rights. We suppose there were three horses, seven kings, civilians, and Dragon girls. The three men compete for the final victory, that is, the crown.
The following story also has a title: Class visibility. If you know the final answer, you can skip the explanation section.
1 <? Php
2
3 class Trones {
4 protected $ fire = "fire ";
5 public $ water = "water ";
6 static private $ trones = "Trones ";
7
8 protected function getFire (){
9 $ this-> fire;
10}
11
12 static public function TheDragenOfMather (){
13 return _ METHOD _. "use". $ this-> getFire (). "gets the". self: getTrones ();
14}
15
16 static public function getWater (){
17 return _ METHOD __;
18}
19
20 static private function getTrones (){
21 return self: $ trones;
22}
23
24}
25
26 class Kings extends Trones {
27 static function TheSevenKing (){
28 return _ METHOD _. "gets the". self: getTrones ();
29}
30}
31
32 class People extends Trones {
33 static function ThePeople (){
34 return _ METHOD _. "gets the". self: getTrones ();
35}
36}
37 echo Kings: TheSevenKing ();
38 echo Trones: TheDragenOfMather ();
39 echo People: ThePeople ();
The correct answer is: the seven countries fought in the fight, there were countless civilian deaths and injuries, and the dragon girl wanted to seize the opportunity to gain profit. Unfortunately, no one finally got the crown and victory. Haha.
When static encounters private, the combination produces complexity and beauty. It is like an abstract person, like the mathematics class that our college teachers talk about. (Netease's public mathematics class is good)
If you want the dragon girl to win the final victory, you just need to help her remove the $ this-> getFire () section of the 13 rows. Similarly, you cannot use any object operator in a static function.
How can people get the crown? Let's go!
If you do not build large frameworks or websites, these concepts such as Interface Implement abstract... You still don't know.
From warcraft