phpcalluserfunc
Ⅰ php如何將變數定義為函數
參見call_user_func_array
網頁鏈接
Ⅱ PHP里如何獲取函數的調用者名稱
先放上來別人的例子吧:
call_user_func函數類似於一種特別的調用函數的方法,使用方法如下:
復制代碼 代碼如下:
function a($b,$c)
{
echo $b;
echo $c;
}
call_user_func('a', "111","222");
call_user_func('a', "333","444");
//顯示 111 222 333 444
?>
調用類內部的方法比較奇怪,居然用的是array,不知道開發者是如何考慮的,當然省去了new,也是滿有新意的:
復制代碼 代碼如下:
class a {
function b($c)
{
echo $c;
}
}
call_user_func(array("a", "b"),"111");
//顯示 111
?>
call_user_func_array函數和call_user_func很相似,只不過是換了一種方式傳遞了參數,讓參數的結構更清晰:
復制代碼 代碼如下:
function a($b, $c)
{
echo $b;
echo $c;
}
call_user_func_array('a', array("111", "222"));
//顯示 111 222
?>
call_user_func_array函數也可以調用類內部的方法的
復制代碼 代碼如下:
Class ClassA
{
function bc($b, $c) {
$bc = $b + $c;
echo $bc;
}
}
call_user_func_array(array('ClassA','bc'), array("111", "222"));
//顯示 333
?>
call_user_func函數和call_user_func_array函數都支持引用,這讓他們和普通的函數調用更趨於功能一致:
復制代碼 代碼如下:
function a($b)
{
$b++;
}
$c = 0;
call_user_func('a', $c);
echo $c;//顯示 1
call_user_func_array('a', array($c));
echo $c;//顯示 2
另:call_user_func函數和call_user_func_array函數都支持引用。
復制代碼 代碼如下:
<?php
function increment(&$var)
{
$var++;
}
$a = 0;
call_user_func('increment', $a);
echo $a; // 0
call_user_func_array('increment', array(&$a)); // You can use this instead
echo $a; // 1
?>
Ⅲ php7 linux上使用 call_user_func_array 報錯
php __call()與call_user_func_array()理解 1. mixed __call ( string name, array arguments )The magic method __call() allows to capture invocation of non existing methods. That way __call() can be used to implement user defined method handling that depends on the name of the actual method being called. This is for instance useful for proxy implementations. The arguments that were passed in the function will be defined as an array in the $arguments parameter. The value returned from the __call() method will be returned to the caller of the method. 譯文: 這個魔術方法允許用戶調用類中不存在的方法,它用於實現那些 依賴於在被調用時的真正方法名的方法. 典型的例子是用來實現代理. 方法的參數$arguments是一個數組 ,__call()的返回值返回給方法調用者白話文: 這個方法主要是用來實現動態方法調用, 如果再一個類定義了__call()這個方法, 當用戶調用這個類的一個不存在的方法時,他可以使用調用的那個不存在的方法的方法名和參數做出用戶定義在__call()方法體內的相應操作,此時__call()方法的參數就是被調用的那個不存在的方法的方法名和參數例子<?phpclass Person{function talk( $sound ){echo $sound;}function __call( $method , $args ){echo 'you call method ' . $method . '
';echo 'and the arguments are
';var_mp( $args );}}$person = new Person();$person->test( 1 , TRUE );?>程序輸出引用you call method testand the arguments are array 0 => int 1 1 => boolean true2. mixed call_user_func_array ( callback function, array param_arr )Call a user defined function with the parameters in param_arr. 參數functionThe function to be called. param_arrThe parameters to be passed to the function, as an indexed array. 返回值Returns the function result, or FALSE on error. 此方法可以通過傳入類名,類中得方法名和方法參數達到動態調用方法的效果例子<?php class Person{function talk( $sound ){echo $sound;}function __call( $method , $args ){echo 'you call method ' . $method . '
';echo 'and the arguments are
';var_mp( $args );}} $person = new Person();call_user_func_array( array( $person , 'talk' ) , array( 'hello' ) );?>程序輸出引用hello兩個方法共用,實現代理模型 class Person{function talk( $sound ){echo $sound;}function __call( $method , $args ){echo 'you call method ' . $method . '
';echo 'and the arguments are
';var_mp( $args );}}class PersonProxy{private $person;function __construct(){$this->person = new Person();}function __call( $method , $args ){call_user_func_array( array( $this->person , $method ) , $args );}}$person_proxy = new PersonProxy(); $person_proxy->talk( 'thank you' );程序輸出引用thank yo