Home  >  Article  >  Backend Development  >  Basic knowledge of PHP (2)_PHP tutorial

Basic knowledge of PHP (2)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:51:32896browse

Object OOP
1.Fatal error: Using $this when not in object context
This mistake is definitely easy to happen when you first learn OOP, because there is a concept that you don’t really understand. The accessibility of a class can also be said to be scope. You can also think of a Chinese person abroad. He does not belong to any culture and does not speak foreign languages ​​(maybe he knows a little bit); but he cannot pass himself. Communicate with foreigners because they were not born in the same country.
So how does the error occur? Look at the example below:

1 2 class Trones{
3  static public $fire = "I am fire.";
4 public $water = "I am water";

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 "
" ;
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 mistake is very classic and very practical. Let’s look at the definition of static first:
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).
Translation: Defining the properties or methods of a class as static allows them to be accessed directly without the need to initialize a class. A property defined as static cannot be accessed by objects of the class using the object operator * -> * (it can be accessed through static methods).
Example:
Lines 7 and 10 made the same mistake. The first one was to use the object operator to access static variables. If you look at the definition, $this is a pseudo variable equivalent to object, an instance. If you use the object operator -> to access it, an error will be reported.
Likewise, you cannot use the static operator :: to access a public variable. The correct access should be lines 14 and 25, one is accessed in the definition of the class (self:: === Trones::), and the other is accessed outside the class.
For inherited classes, the same rules above apply.
-------------------------------------------------- ----Separating line-------------------------------------------------- ------------------------------------------
2.Fatal error: Call to private method
There is a very interesting TV series recently called Game of Thrones. Let’s assume that there are three parties, seven kings, common people, and a dragon lady. The three of them competed below for the final victory, which was the crown.
The following story also has a title: Class Visibility (Visibility) If you know the final answer, you can skip the explanation part.


1 2
3 class Trones {
4     protected $fire = " fire ";
5 public $water = " water " ;
6 static private $trones = "Trones";

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 7-nation war was fought internally, resulting in numerous civilian casualties. The Dragon Girl wanted to take advantage of the situation, but unfortunately, no one got the crown or victory in the end. Ha ha.
When static meets private, the combination produces complexity and beauty; just like abstract people, like the mathematics classes taught by our university teachers; (but NetEase’s public mathematics classes are very good)
If you want Dragon Girl to win the final victory, you just need to help her remove the $this->getFire() part on line 13. In the same way you cannot use any object operators in a static function.
How do you get the crown for the people? Go and fight for it!

If you don't build large frameworks and websites these concepts like Interface Implement abstract. . . It's better that you don't know.

Excerpted from warcraft

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478186.htmlTechArticleObject OOP 1.Fatal error: Using $this when not in object context This error is definitely easy to occur when you just learn OOP. Because there is a concept that you don’t really understand. Class accessibility (accessi...
Statement:
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