php call_user_func的简介

WBOY
Release: 2016-06-23 14:30:42
Original
1016 people have browsed it

mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )

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

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

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

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

 

<?php // 普通的回调函数function my_callback_function() {    echo 'hello world!';}// 回调方法class MyClass {    static function myCallbackMethod() {        echo 'Hello World!';    }}// Type 1: 简单的回调call_user_func('my_callback_function'); // Type 2: 静态类回调call_user_func(array('MyClass', 'myCallbackMethod')); // Type 3: 类方法回调$obj = new MyClass();call_user_func(array($obj, 'myCallbackMethod'));// Type 4: 调用类方法 (As of PHP 5.2.3)call_user_func('MyClass::myCallbackMethod');// Type 5: 相对静态类方法的调用(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?>
Copy after login

  

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!