PHP Variable function usage analysis

Source: Internet
Author: User
PHP supports the concept of mutable functions. This means that if aVariable name with parentheses, PHP looks for a function with the same name as the value of the variable, and tries to execute it. Variable functions can be used to implement includingcallback function , a function table, for some purposes.

Variable functions cannot be used for language structures such as echo (), print (), unset (), Isset (), empty (), include (), require (), and similar statements . These structures need to be used as variable functions using their own wrapper functions.
Example #1 Variable Function example

<?phpfunction  foo () {    echo  "in foo () <br/>/n";} function  Bar ($arg  = "  ) {    echo" in  bar (); argument is ' $arg '. <br/>/n ";} Use Echo's wrapper function  Echoit ($string) {    echo  $string;} $func  =  ' foo '; $func ();         This calls foo () $func  =  ' bar '; $func (' Test ');   This calls bar () $func  =  ' Echoit '; $func (' Test ');   This calls Echoit ()?>

You can also use the properties of a mutable function to invoke a method of an object.


Example #2 Variable Method Example

<?phpclass  foo{    function  Variable ()    {         $name  =  ' Bar ';         $this $name ();  This calls the bar () method     }    function  Bar ()    {        echo  ' this is Bar ';    }} $foo  = new  foo (); $funcname  =  "Variable"; $foo-$funcname ();    This calls $foo->variable ()?>

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.