PHP--Magic Method call Method: __call (), __callstatic ()

Source: Internet
Author: User

Method overloading: The __call () method is called automatically when a method that does not exist or has insufficient permissions is called

Public Function __call ($name, $args) {}: Invoke method with Object

public static function __callstatic ($name, $args) {}: Calling a static method with a class

$name: Called Method name

$args: An array of parameter lists for the called method

<?PHP//php Method Overloading    classperson{//Method        Private functionTest () {Echo __method__; }        protected Static functiontest1 () {Echo __method__; }        //Add Magic Method//There are two parameters: the first is the method name, the second is the parameter array         Public function__call ($name,$args){            //Var_dump ($name);//Method name//var_dump ($args);//parameter array//define Allow access to list            $allow=Array(' Test '); if(In_array($name,$allow))                $this-$name();//Variable Method            Else return false; }        //add access to a static method         Public Static function__callstatic ($name,$args){            $allow=Array(' Test1 '); if(In_array($name,$allow)) self::$name();//Variable Method            Else return false; }    }    Echo' <pre> '; //instantiation of    $person=NewPerson ; //Access Private method//No Magic method before//$person->test ();            Error//person::test1 (); Error    $person->test ();//with the __call magic method, and allow access to    Var_dump($person->test2 ());//false Person:: Test1 ();

PHP--Magic Method call Method: __call (), __callstatic ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.