PHP Variable Functions

Source: Internet
Author: User

PHP supports the concept of variable functions. This means that if a variable name has parentheses
The system looks for a function with the same name as the variable value and tries to execute it. Variable functions can be used for some purposes, including callback functions and function tables.

Variable functions cannot be used in language structures, such as ECHO ()

, Print ()

, Unset ()

, Isset ()

, Empty ()

, Include ()

, Require ()

And similar statements. You need to use your own packaging functions to use these structures as variable functions.

 

Example #1 Variable Function example

<? PHP

Function
Foo
(){
Echo
"In Foo () <br/>/N"
;
}

Function
Bar
(
$ ARG
=
''
){
Echo
"In bar (); argument was'
$ ARG
'. <Br/>/N"
;
}


// Use echo's packaging function

Function
Echoit
(
$ String
)
{
Echo
$ String
;
}


$ Func
=
'Foo'
;

$ Func
();
// This callfoo ()


$ Func
=
'Bar'
;

$ Func
(
'Test'
);
// This callbar ()


$func 
= 
'echoit'
;

$func
(
'test'
);  
// This calls echoit()

?>

You can also call an object method by using the features of a variable function.

Example #2 variable method example

<?php

class 
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.