Magic Method __call () Example (PHP Object-oriented Advanced tutorial)

Source: Internet
Author: User
What is the __call () Magic method?

__call is one of the magic methods that calls the __call method when a program calls a method that is not declared in the current class or that does not have permission to call it. The __call () method contains two parameters, the method name and the method parameter. Where the method parameter is in the form of an array.

Here's a simple example to help you understand:

<?phpheader ("Content-type:text/html;charset=utf-8"); class Sport{public function Run () {echo ' method exists directly calling me ';} function __call ($name, $arguments) {echo ' method does not exist call me '; Echo ' <br/> '; Echo ' method named: '. $name; Echo ' <br/> '; Echo ' parameter is:<pre> '; Var_dump ($arguments);}} $sport = new Sport (); $sport->run (); Echo ' <br/> '; $sport->football (' David Beckham ', 30);

We create a class that creates a run method and a magic Method __call () in the class. Next, we instantiate the object $sport call two methods, one is the run () method that exists in the class, and one is the football () method that does not exist. You can run the code to see the results.

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.