类型
在线手册:中文 英文
PHP手册

本文档中使用的伪类型

mixed

mixed 说明一个参数可以接受多种不同的(但并不必须是所有的)类型。

例如 gettype() 可以接受所有的 PHP 类型,str_replace() 可以接受字符串和数组。

number

number 说明一个参数可以是 integer 或者 float

callback

有些诸如 call_user_function()usort() 的函数接受用户自定义的函数作为一个参数。Callback 函数不仅可以是一个简单的函数,它还可以是一个对象的方法,包括静态类的方法。

一个 PHP 函数用函数名字符串来传递。可以传递任何内置的或者用户自定义的函数,除了语言结构如 array()echo()empty()eval()exit()isset()list()print()unset()

一个对象的方法以数组的形式来传递,数组的下标 0 指明对象名,下标 1 指明方法名。

对于没有实例化为对象的静态类,要传递其方法,将数组 0 下标指明的对象名换成该类的名称即可。

除了普通的用户定义的函数外,也可以使用create_function()来创建一个匿名的回调函数(callback)。

Example #1 回调函数(callback)示例

<?php 

// 普通的回调函数
function my_callback_function() {
    echo 
'hello world!';
}

// 回调方法
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}

// Type 1: Simple callback
call_user_func('my_callback_function'); 

// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod')); 

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

// Type 5: Relative static class method call (As of PHP 5.3.0)
class {
    public static function 
who() {
        echo 
"A\n";
    }
}

class 
extends {
    public static function 
who() {
        echo 
"B\n";
    }
}

call_user_func(array('B''parent::who')); // A
?>

Note: 在 PHP4 中,必须使用引用来创建一个指向实际object,而不是它的一个拷贝。详情请见引用的解释。 In PHP4, it was necessary to use a reference to create a callback that points to the actual object, and not a copy of it. For more details, see References Explained.

void

void 作为返回类型意味着函数的返回值是无用的。void作为参数列表意味着函数不接受任何参数。

...

在函数原型中,$... 表示等等的意思。当一个函数可以接受任意个参数时使用此变量名。


类型
在线手册:中文 英文
PHP手册
PHP手册 - N: 本文档中使用的伪类型

用户评论:

liam at helios-sites dot com (06-Dec-2010 12:44)

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'));

michael dot martinek at gmail dot com (29-Aug-2009 05:20)

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.

phpguy at lifetoward dot com (12-Jun-2009 01: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.

sahid dot ferdjaoui at gmail dot com (20-Apr-2009 11:19)

An example with PHP 5.3 and lambda functions

<?php

  array_map
(function ($value) {
    return new
MyFormElement ($value);
  },
$_POST);

?>

Hayley Watson (24-May-2007 06:44)

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.

levi at alliancesoftware dot com dot au (08-Feb-2007 10:44)

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
<?
 // 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');
?>

Edward (01-Feb-2007 10:15)

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:
<?
array_map(intval, $array)
?>

static functions in a class:
<?
array_map(array('MyClass', 'MyFunction'), $array)
?>

functions from an object:
<?
array_map(array($this, 'MyFunction'), $array)
?>

I hope this clarifies things a little bit