Home > Backend Development > PHP Tutorial > Example of php calling function through string_PHP tutorial

Example of php calling function through string_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:36:59
Original
865 people have browsed it

1. call_user_func

Copy code The code is as follows:

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’s strange to call the method inside the class. It actually uses array. I don’t know what the developer thought about it. Of course, new is omitted, which is also full of novelty:

Copy code The code is as follows:

class a {
function b($c){
echo $ c;
}
}
call_user_func(array("a", "b"),"111");

//Display 111
?>

2. call_user_func_array

The call_user_func_array function is very similar to call_user_func, except that the parameters are passed in a different way to make the structure of the parameters clearer:

Copy code Code As follows:

function a($b, $c){
echo $b;
echo $c;
}
call_user_func_array('a', array( "111", "222"));

//Display 111 222
?>


The call_user_func_array function can also call methods inside the class
Copy code The code is as follows:

Class ClassA{
function bc($b, $c) {
$bc = $b + $c;
echo $bc;
}
}
call_user_func_array(array('ClassA','bc'), array("111", "222") );

//Display 333
?>

Both the call_user_func function and the call_user_func_array function support references, which makes them more functionally consistent with ordinary function calls:

Copy code The code is as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736827.htmlTechArticle1. call_user_func Copy code The code is as follows: 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 ? Call...
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template