本文档中使用的伪类型

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

例子 11-13. 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'));
?>


add a note add a note User Contributed Notes
sworley at dotster dot com
31-Oct-2006 10:03
You can use parent:: and self:: in a callback, as in <? array(&$object, "parent::methodname") ?>, only in PHP 5.1.2 and above.
mr dot lilov at gmail dot com
12-Aug-2005 09:17
This's a useful example about callback, Look at the session_set_save_handler function.

From: http://www.zend.com/zend/spotlight/code-gallery-wade8.php

<?php

/* Create new object of class */
$ses_class = new session();

/* Change the save_handler to use the class functions */
session_set_save_handler (array(&$ses_class, '_open'),
                         array(&
$ses_class, '_close'),
                         array(&
$ses_class, '_read'),
                         array(&
$ses_class, '_write'),
                         array(&
$ses_class, '_destroy'),
                         array(&
$ses_class, '_gc'));

/* Start the session */
session_start();

class
session
{
  
/* Define the mysql table you wish to use with
       this class, this table MUST exist. */
  
var $ses_table = "sessions";

  
/* Change to 'Y' if you want to connect to a db in
       the _open function */
  
var $db_con = "Y";

  
/* Configure the info to connect to MySQL, only required
       if $db_con is set to 'Y' */
  
var $db_host = "localhost";
   var
$db_user = "username";
   var
$db_pass = "password";
   var
$db_dbase = "dbname";

  
/* Create a connection to a database */
  
function db_connect() {
   ............
   }

  
/* Open session, if you have your own db connection
       code, put it in here! */
  
function _open($path, $name) {
   .............
   }

  
/* Close session */
  
function _close() {
   ..............
   }

  
/* Read session data from database */
  
function _read($ses_id) {
   .................
   }

  
/* Write new data to database */
  
function _write($ses_id, $data) {
   ...........
   }

  
/* Garbage collection, deletes old sessions */
  
function _gc($life) {
   ............
   }
}
?>
strauchdieb at gmx dot de
03-Aug-2005 06:13
Don't forget the reference operator (&) when using an object method as a callback! If you forget the '&', $this will probably point to an other object than the one you intended.