PHP Magic Method __call

Source: Internet
Author: User
The member method does not exist when the Magic method is automatically called, PHP does not support polymorphism, can be implemented by call pseudo polymorphic.

<?
Class a{
Function Show () {
Echo 1;
}
function call ($f, $args) {
echo "__call";
}
}
$obj = new A;
$obj->k ();
?>

Output __call

That is, when an object is instantiated, its member method is called directly when the member method of the object is called, but when the member method does not exist, the call method is called instead.
At the same time call must have two parameters, this parameter name can be arbitrarily taken, but for convenience we usually take as $name and $args

When the member method does not exist, call is called, and the non-existent method name is passed as a string to the first argument (where the first argument is $name), and the argument passed as the invocation is passed as a list to the second parameter (here the second argument is $args).

PHP Magic Method __call

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.