PHP six magic methods

  1. __ construct(), constructor of class
  2. __ destruct(), destructor of class
  3. __ call () calls when an invocable method is invoked in an object.
  4. __ callStatic () calls when an invocable method is invoked in a static way.
  5. __ get() is called when the member variable of a class is obtained
  6. __ set() is called when setting the member variable of a class

I__ construct(), constructor of class

When creating a new object, this function will be called automatically to initialize the properties of the class that need to be initialized.

effect:

Do some initialization operations, such as initializing the properties in the class

Format of declaration in class:

public function __construct([Parameters passed in])
{
     Function body, operation to be performed
}

Parameters are passed in when a new object is created outside the class, for example:

$OBJ = new Class name(Fill in the parameters to be initialized)

example:
class Person{
    public $name;
    public $sex;
    
    public function __construct($name,$sex){
        //Initialize the attribute to be initialized
        $this->name=$name;
        $this->sex=$sex;
        echo $this->name,$this->sex; 
    }

}
//create object
$OBJ = new Person('Zhang San','male');

Note: only one constructor can be declared in a class because PHP does not support constructor overloading

II__ destruct(), destructor of class

After executing the script language in all classes, this destructor is called automatically. The destructor method allows some operations or functions to be performed before destroying a class, such as closing files, releasing result sets, etc.

effect:

In a class, it is usually used to complete some operations before destroying a class to free up space and do cleaning tasks

Format declared in class:

public function __destruct(){
    Function body, operations to be performed, such as closing files, releasing result sets, etc
}

Note: destructors cannot take any parameters

III__ call () calls when an invocable method is invoked in an object.

When the method you get does not exist or has no declaration, this method will be called automatically

The method contains two parameters.

The first parameter: when you want to get an undeclared method in the class, this parameter will get the method name you get;

Second parameter: when you want to get an undeclared method in the class, this parameter will get all the parameters of the method you get. The second parameter is an array type;

effect:

To prevent accidental termination of the program when calling non-existent or undeclared methods

Format declared in class:

public function __call($name,$value){
    Function body, operations to be performed, such as telling a sentence that the method you access is not declared, etc
}

Reference code:

class Person{
    public function _call($name,$value){
        echo "You visited{$name}Method not declared";
    }
}
$obj = new Person();
//Calling an undeclared method
$obj->sey();




Output result: the information you visited sey Method not declared

IV__ callStatic () calls when an invocable method is invoked in a static way.

This method is the same as above__ call() function__ callStatic() is the same except that it is prepared for static methods.

Reference code:

class Person{
    public static function __callStatic($name,$value){
        echo "You visited{$name}Static method not declared";
    }
}
$obj = new Person();
//Calling a static method without a declaration
$obj->sey();




Output result: the information you visited sey Static method not declared

V__ get() is called when the member variable of a class is obtained

Private in class   When the declared property is called outside the class, it will have a fatal error of "unable to access a private property", which can be used__ get() magic method to solve

effect:

In the process of program running, get the value of private attribute externally. You can get the attribute value you want to get in the class through this magic method

  Format declared in class:

public function __get($name){
    Function body, operations to be performed, such as:
    return $this->Private property name;
}

Reference code:

class Person{
    private $sex='male';
    //Parameter gets the name of the private property of the external call
    public static function __get($name){
        return $this->ses;
    }
}
$obj = new Person();
//Calling private properties in a class
echo $obj->sex;




Output result: Male

Vi__ set() is called when setting the member variable of a class

effect:

__ The set ($property, $value) ` method is used to set private properties. When assigning a value to an undefined property, this method will be triggered, and the parameters passed are the set property name and value.

  Format declared in class:

public function __set($name,$value){
    Function body, operations to be performed, such as:
    $this->$name=$value;
    return $this->Private property name
}

Reference code:

class Person{
    private $sex='male';
    //Parameter gets the name of the private attribute of the external call and the attribute value to be modified
    public static function __set($name,$value){
        $this->$name=$value;
        return $this->ses;
    }
}
$obj = new Person();
//Calling private properties in a class
echo $obj->sex='female';




Output: Female

Keywords: PHP

Added by phpnoobie9 on Sat, 20 Nov 2021 02:32:51 +0200