Several common magic methods and magic variable analysis in PHP

Posted by Tedglen2 on Sun, 01 Dec 2019 22:31:06 +0100

Original address: http://small.aiweimeng.top/index.php/archives/49.html

Let's not talk about it first, but go to the code directly, as follows:

 1 class Demo
 2 {
 3     private $str = 'str';
 4    
 5     //Auto load on instantiation function
 6     public function __construct()
 7     {
 8         echo "start<br/>";
 9     }
10 
11     //__call()Used to get undefined function
12     public function __call($name, $arguments)
13     {
14         echo $name.'_call<br>';
15     }
16 
17     //Get private variable
18     public function __get($name)
19     {
20         // TODO: Implement __get() method.
21         echo $this->$name.'_get<br/>';
22     }
23 
24     //By keyword clone Called by an object when it is cloned__clone()Method
25     public function __clone()
26     {
27         // TODO: Implement __clone() method.
28     }
29 
30     //__set()Set the value of a private variable
31     public function __set($name, $value)
32     {
33         // TODO: Implement __set() method.
34         $this->$name = $value;
35     }
36 
37     //--callStatic()Call not defined static static state function
38     public static function __callStatic($name, $arguments)
39     {
40         // TODO: Implement __callStatic() method.
41         echo $name.'_classStatic';
42     }
43 
44     //Called automatically when class objects are deleted
45     public function __destruct()
46     {
47         // TODO: Implement __destruct() method.
48         echo "end";
49     }
50 
51 
52 }
53 
54 $class = new Demo();
55 $class->success();
56 $class->succ = 111;
57 echo $class->succ;
58 echo $class->str;
59 echo '<br>';
60 $obj = clone $class;
61 print_r($obj);
62 echo '<br>';
63 $class::end();


Operation result:

start
success_call
111str_get

clone
Demo Object ( [str:Demo:private] => str [succ] => 111 ) 
end_classStatic
endend

Method summary:

1. Set(), get(), isset(), unset() can be classified into one class, which is applicable to the setting, value taking, judgment and deletion of private variables.
2. The constructor and destructor of the struct () are generated when instantiating a class. They are different in that they are constructed at the front,
Destructor at the back
3. When calling a method that is not defined in class, an error will be reported. If the class has a call() defined, the call() method will be called directly for operation.
For example: $class - > success ('data '); the call method in the class starts to convert the parameter to array ([0] = > data');
__The callStatic() method works the same way, only for static methods that are not defined.

 

Several common magic variables:

 1 namespace app;
 2 
 3 //__LINE__ Current script line number
 4 echo __LINE__.'<br/>';
 5 
 6 //__FILE__ Full path and file name of the file
 7 echo __FILE__.'<br/>';
 8 
 9 //__DIR__ File directory
10 echo __DIR__.'<br/>';
11 
12 class Test {
13     function demo(){
14         //__FUNCTION__ Function name, php5 Returns the name of the function when it is defined (case sensitive)
15         echo __FUNCTION__.'<br/>';
16 
17         //__CLASS__ Class names PHP 5 This constant returns the name of the class when it is defined (case sensitive).
18         //Note that since PHP 5.4, CLASS has also played a role in trait.
19         //When used in a trait method, CLASS is the name of the CLASS that calls the trait method.
20         echo __CLASS__.'<br/>';
21 
22         //__METHOD__ Class, returns the name of the method when it was defined (case sensitive)
23         echo __METHOD__.'<br/>';
24 
25 
26         //__NAMESPACE__ Current namespace
27         echo __NAMESPACE__.'<br/>';
28     }
29 }
30 
31 (new Test())->demo();
32 
33 trait HelloWorld {
34     public function sayHello() {
35         //__TRAIT__ Trait Name  PHP 5.4 From this constant return trait Name when defined (case sensitive)
36         echo __TRAIT__.'<br/>';
37     }
38 }
39 
40 class TheWorldIsNotEnough {
41     use HelloWorld;
42 }
43 $o = new TheWorldIsNotEnough();
44 $o->sayHello();

Output results:

12
G:\phpstudy\PHPTutorial\WWW\phpDemo\03-08.php
G:\phpstudy\PHPTutorial\WWW\phpDemo
demo
app\Test
app\Test::demo
app
app\HelloWorld

 

Topics: PHP