PHP calls a function through a string

Source: Internet
Author: User
PHP calls the function via a string from: ziming.orgarchives6695.html? 1. call_user_funcfunctiona ($ B, $ c) {echo $ B; echo $ c;} call_user_func (a, 111,222); call_user_func (a, 333,444); PHP calls a function through a string

From: http://ziming.org/archives/6695.html

?

1. call_user_func

Function a ($ B, $ c) {echo $ B; echo $ c;} call_user_func ('A', "111", "222"); call_user_func ('A ', "333", "444 ");? // Display 111 222 333 444?>? // It is strange to call the internal methods of the class. actually, array is used, and developers do not know how to consider it. of course new is saved, which is also innovative: class a {function B ($ c) {echo $ c;} call_user_func (array ("a", "B"), "111 ");? // Display the 111?>

2. call_user_func_array

The call_user_func_array function is similar to call_user_func, but the parameter is passed in another way to make the parameter structure clearer:

Function a ($ B, $ c) {echo $ B; echo $ c;} call_user_func_array ('A', array ("111", "222 "));? // Display 111 222?>? // Can the call_user_func_array function call internal methods of the class? Class ClassA {function bc ($ B, $ c) {$ bc = $ B + $ c; echo $ bc;} call_user_func_array (array ('classa ', 'BC'), array ("111", "222 "));? // Display the 333?>

Call_user_func and call_user_func_array functions support reference, which makes them more functional than normal function calls:

Function a (& $ B) {$ B ++;} $ c = 0; call_user_func ('A', & $ c); echo $ c; // Display 1call_user_func_array ('A', array (& $ c); echo $ c; // Display 2

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.