PHP common use of magic methods such as __construct and __call()

Common magic methods

Reference: PHP commonly used magic methods and rules

1.__construct A class with a constructor will first call this method every time a new object is created; the initialization work is performed.
3. __call() When calling an inaccessible method in an object, __call() will be called.
4. __callStatic() When an inaccessible method is called in a static context, __callStatic() will be called.
5.__set() is called when assigning a value to an inaccessible attribute
6.__get() is automatically called
when reading an inaccessible attribute value 7.__isset() is automatically called when isset or empty is used for an inaccessible private attribute
8. __unset() When unset is used for inaccessible private properties;
9.__toString() is called automatically when an instance object of a class; is called when output as a string

Example: (I used the controller of laravel directly to do the experiment, the dump() in the example is the breakpoint function of laravel)
1. Parent class:

class ParentController
{
    
    
    private $name = 'xiaochi';

    public function __construct($data = [])
    {
    
    
        dump('调用父类 __construct 构造函数');
    }

    private function syfun()
    {
    
    
        dump('这是私有方法');
    }

    private static function syfunStatic()
    {
    
    
        dump('这是静态私有方法');
    }

    //访问私有方法
    public function __call($method, $arg_array)
    {
    
    
        dump("访问私有方法:".$method." ,自动调用了父类__call方法");
        return $this->$method();
    }

    //访问静态私有方法
    public static function __callStatic($method, $arg_array)
    {
    
    
        dump("访问静态私有方法:".$method." ,自动调用了父类__callStatic方法");
        return self::$method();
    }

    //获取私有属性
    public function __get($attribute){
    
    
        dump("访问私有属性:".$attribute.",自动调用了父类__get()方法");
        if(isset($this->$attribute)){
    
    
            return $this->$attribute;
        } else{
    
    
            dump($attribute);
            return $attribute;
        }
    }

    //设置私有属性
    public function __set($attribute,$val){
    
    
        dump("赋值私有属性:".$attribute." 值:" .$val." ,自动调用了父类__set()方法为私有属性赋值");
        $this->$attribute = $val;
    }

    //  当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用。
    public function __isset($attribute)
    {
    
    
        dump("是否定义 ".$attribute." 值?\n");
    }

    // 当对不可访问属性调用 unset()会被调用
    public function __unset($attribute)
    {
    
    
        dump("销毁私有变量 ".$attribute." 时调动");
    }

    // 当类 被当成字符串时 会被调用
    public function __toString() {
    
    
        return __CLASS__.'类 被当做字符串调用';
    }
}

Subclass:

class TestController extends ParentController
{
    
    
    public function __construct($data = [])
    {
    
    
        //子类的构造方法中不会自动的调用父类的构造方法 		  
        parent::__construct();  //执行父类的构造函数
        dump('调用子类 __construct 构造函数');
    }

    private $name2 = 'cyk';

    public function magicFun()
    {
    
    
        echo $this->name;  //获取父类private私有属性值自动调用父类__get()方法
        $this->name = $this->name2;  ///设置父类private私有属性值自动调用父类__get()方法
        echo $this->name;

        $this->syfun();
        ParentController::syfunStatic();

        dump(isset($this->name));
        dump(empty($this->name));
        unset($this->name);

        echo (new ParentController());
    }

}

The following is the execution result:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39004843/article/details/105685839