PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

类型戏法> <NULL
Last updated: Sun, 25 Nov 2007

view this page in

本文档中使用的伪类型

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 下标指明的对象名换成该类的名称即可。

Example#1 Callback 函数实例

<?php
// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}

// An example callback method
class MyClass {
    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'));
?>



类型戏法> <NULL
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
本文档中使用的伪类型
Hayley Watson
24-May-2007 01: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
09-Feb-2007 06: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 06: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

类型戏法> <NULL
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites