當前位置:編程學習大全網 - 編程語言 - php7 linux上使用 call_user_func_array 報錯

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_dump( $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_dump( $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_dump( $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

  • 上一篇:學習嵌入式Linux應用開發完全手冊
  • 下一篇:王小波的計算機水平有多好?
  • copyright 2024編程學習大全網