spl_autoload_register array参数

spl_autoload_register

(PHP 5 >= 5.1.2)

spl_autoload_register — 注册给定的函数作为 __autoload 的实现

说明 ¶

bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool$prepend = false ]]] )
 
callable定义:

Callback 回调类型 ¶

自 PHP 5.4 起可用 callable 类型指定回调类型 callback。本文档基于同样理由使用 callback 类型信息。

一些函数如 call_user_func() 或 usort() 可以接受用户自定义的回调函数作为参数。回调函数不止可以是简单函数,还可以是对象的方法,包括静态类方法。

传递 ¶

一个 PHP 的函数以 string 类型传递其名称。可以使用任何内置或用户自定义函数,但除了语言结构例如:array()echoempty()eval()exit()isset()list()print 或 unset()

一个已实例化的对象的方法被作为数组传递,下标 0 包含该对象,下标 1 包含方法名。

静态类方法也可不经实例化该类的对象而传递,只要在下标 0 中包含类名而不是对象。自 PHP 5.2.3 起,也可以传递'ClassName::methodName'

除了普通的用户自定义函数外,create_function() 可以用来创建一个匿名回调函数。自 PHP 5.3.0 起也可传递 closure给回调参数。

<?php 

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

// An example callback method
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 A {
    public static function who() {
        echo "A\n";
    }
}

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

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

 


posted @ 2015-03-19 10:11  lein.wang  Views(176)  Comments(0Edit  收藏  举报