1. call_user_func()
调用普通函数 :
<?php function test($name) { echo "hello,{$name}"; } call_user_func('test','test');调用类方法:
使用命名空间: namespace Foobar; class Foo { public static function test() { print "Hello,World"; } } echo __NAMESPACE__.'\Foo::test';die; call_user_func(__NAMESPACE__.'\Foo::test'); <?php class myclass { public static function hello($name) { echo "hello,{$name}"; } } call_user_func(array('myclass','hello'),'www'); class myclass1 { public static function hello1($name,$age) { echo "hello,{$name},{$age}"; } } call_user_func(array('myclass1','hello1'),'wjh',100); 数组里面放类名跟方法,外面放参数。2.call_user_func_array()
<?php function test($name, $age) { echo "hello,{$name},{$age}"; } call_user_func_array('test',array('www',100)); class foo { public static function bar($arg1, $arg2) { echo "{$arg1},{$arg2}"; } } call_user_func_array(array('foo','bar'),array('aaa','bbb')); call_user_func() 与 call_user_func_array() ,区别,后者参数可以用 数组形式。利用函数func_get_args()和call_user_func_array() 进行overload
<?php function otest1 ($a) { echo( '一个参数' ); } function otest2 ( $a, $b) { echo( '二个参数' ); } function otest3 ( $a ,$b,$c) { echo( '三个啦' ); } function otest () { $args = func_get_args(); $num = func_num_args(); call_user_func_array( 'otest'.$num, $args ); } otest(1,2,3);3. func_get_arg(), func_get_args(), func_num_args()
<?php function test($a, $b, $c) { $a = func_get_args(); // 获取所有参数 var_dump($a); $b = func_num_args(); // 获取参数数量 var_dump($b); echo func_get_arg(0),'<br/>'; // 获取某个参数 echo func_get_arg(1),'<br/>'; echo func_get_arg(2),'<br/>'; } test('a','b','c');
