©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
mixed 说明一个参数可以接受多种不同的(但不一定是所有的)类型。
例如 gettype() 可以接受所有的 PHP 类型, str_replace() 可以接受字符串和数组。
number 说明一个参数可以是 integer 或者 float 。
本文档中在 PHP 5.4 引入 callable 类型之前使用 了 callback 伪类型。二者含义完全相同。
void 作为返回类型意味着函数的返回值是无用的。void 作为参数列表意味着函数不接受任何参数。
在函数原型中,$...
表示等等的意思。当一个函数可以接受任意个参数时使用此变量名。
[#1] mike@EastGhostCom [2012-08-13 05:50:38]
If you pass a string as the callback function (i.e., 2nd parm to preg_replace_callback()), then PHP will interpret it as a function's name in the current scope -- and Main::dada_cb is not a valid function name in any scope.
If you want to specify a static method of a class as the callback (i.e., "Main::dada_cb"), then you must pass as 2nd parm to preg_replace_callback:
array( 'Main', 'dada_cb')
And, if you want to use as a callback some method of an instantiated object (i.e., $object->dada_cb), then you must pass as the 2nd parm to preg_replace_callback:
array( $object, 'dada_cb' )
[#2] liam at helios-sites dot com [2010-12-06 04:44:43]
Note that (e.g.) usort calls on static methods of classes in a namespace need to be laid out as follows:
usort($arr, array('\Namespace\ClassName', 'functionName'));
[#3] michael dot martinek at gmail dot com [2009-08-29 09:20:46]
The documentation is a little confusing, and with the recent OO changes it adds a little more to the confusion.
I was curious whether you could pass an object through the user func, modify it in that callback and have the actual object updated or whether some cloning was going on behind the scenes.
<?php
class Test
{
var $sValue = 'abc';
function testing($objTest)
{
$objTest->sValue = '123';
}
}
$obj = new Test();
call_user_func(array($obj, 'testing'), $obj);
var_dump($obj);
?>
This works as expected: The object is not cloned, and $sValue is properly set to '123'. With the OO changes in PHP 5, you don't need to do "function testing(&$objTest)" as it is already passed by reference.
[#4] phpguy at lifetoward dot com [2009-06-11 17:44:44]
I noticed two important thing about putting callbacks into an arg list when calling a function:
1. The function to which the callback refers must be defined earlier in the source stream. So for example:
function main() {...; usort($array, 'sortfunction'); ... }
function sortfunction($a, $b){ return 0; }
Will NOT work, but this will:
function sortfunction($a, $b){ return 0; }
function main() {...; usort($array, 'sortfunction'); ... }
2. It's not really just a string. For example, this doesn't work:
usort($array, ($reverse?'reversesorter':'forwardsorter'));
I found these two discoveries quite counterintuitive.
[#5] sahid dot ferdjaoui at gmail dot com [2009-04-20 03:19:31]
An example with PHP 5.3 and lambda functions
<?php
array_map (function ($value) {
return new MyFormElement ($value);
}, $_POST);
?>
[#6] Hayley Watson [2007-05-23 22:44:32]
The mixed pseudotype is explained as meaning "multiple but not necessarily all" types, and the example of str_replace(mixed, mixed, mixed) is given where "mixed" means "string or array".
Keep in mind that this refers to the types of the function's arguments _after_ any type juggling.
[#7] levi at alliancesoftware dot com dot au [2007-02-08 14:44:42]
Parent methods for callbacks should be called 'parent::method', so if you wish to call a non-static parent method via a callback, you should use a callback of
<?php
// always works
$callback = array($this, 'parent::method')
// works but gives an error in PHP5 with E_STRICT if the parent method is not static
$callback array('parent', 'method');
?>
[#8] Edward [2007-02-01 02:15:29]
To recap mr dot lilov at gmail dot com's comment: If you want to pass a function as an argument to another function, for example "array_map", do this:
regular functions:
<?php
array_map(intval, $array)
?>
static functions in a class:
<?php
array_map(array('MyClass', 'MyFunction'), $array)
?>
functions from an object:
<?php
array_map(array($this, 'MyFunction'), $array)
?>
I hope this clarifies things a little bit