Detailed Explanation of php Magic Method

  • 2021-07-26 07:06:10
  • OfStack

From PHP 5 onwards, classes in PHP can use magic methods. It stipulates that methods beginning with two underscores (__) are reserved as magic methods, so it is recommended that everyone's function name should not start with __, unless it is to overload existing magic methods. PHP keeps all class methods beginning with _ _ (two underscores) as magic methods.

__toString () and __invoke ()

public string __toString (void): This method is called automatically when the object is used as string. This method must return 1 string


<?php
    class Magic{
        public function  __tostring(){
            return "hello world!";
        }
    }
    $obj = new Magic();
    echo $obj;//hello world!
?>

__invoke (): This method is automatically called when the object is called as a method.


<?php
    class Magic{
        public function  __tostring(){
            return "hello world!";
        }
        public function __invoke($x){
            echo "__invoke called with param ".$x."\n";
        }
    }
    $obj = new Magic();
    $obj(10);//__invoke called with param 10
?>

__call () and __callStatic ()

__call (): The __call () method is automatically called when an object accesses a method name that does not exist

__callStatic (): The __callStatic () method is automatically called when an object accesses a static method name that does not exist

Through these two methods, the call of the same method name can correspond to different method implementations


<?php
class Magic{
   //`$name` Parameter is the name of the method to call. `$arguments` The parameter is 1 Enumerated array,
  // Contains the method to be passed to `$name` The parameters of the.
        public function __call($name,$arguments){
            //implode() Function to combine array elements into 1 Strings. implode(separator,array)
            echo "Calling " . $name ." with param: ".implode(", ",$arguments)."\n";
        }
    }
$obj = new Magic();
$obj->run("para1","para2");//obj To call run Method, output: Calling run with param: para1, para2  
?>

__get () and __set ()

When you assign a value to an inaccessible property, __set () is called
When reading the value of an inaccessible property, __get () is called


<?php
class Magic{
     //function There must be before static Keyword
    public function __get($name){
        return "Getting the property " . $name;
    }
}
$obj = new Magic();
echo $obj->className."\n";//Getting the property className
?>

When reading the value of an inaccessible property, __get () is called


<?php
class Magic{     public function __set($name,$value){
        echo "Setting the property " . $name ."to value ". $value ."\n";
    }
}
$obj = new Magic();
$obj->className = 'MagicClass';//Setting the property classNameto value MagicClass
?>

__isset () and __unset ()

When you call isset () or empty () on an inaccessible property, __isset () is called
When unset () is called on an inaccessible property, __unset () is called


<?php
class Magic{
    public function __isset($name){
        echo "__isset invoked\n";
        return true;
    }
}
$obj = new Magic();
echo '$obj->className is set?'.isset($obj->className)."\n";//__isset invoked     $obj->className is set?1
?>

Above is 8 php object-oriented magic method introduction and example, I hope you can help


Related articles: