php 回呼函數(callback)

來源:互聯網
上載者:User

標籤:style   blog   使用   io   2014   cti   ar   new   

下面的內容是來自php.net 官方文檔

callbacks

call_user_func() ,usort()等函數接受使用者自訂的回呼函數作為參數.

回呼函數可以不僅僅是簡單的函數,也可以是對象方法,包括對象的靜態方法.

php 函數可以通過字串類型的函數名進行調用,任何內建的或者使用者自訂的函數都可以被使用.但是這其中不包括

php的語言結構,比如:array(),echo,empty(),eval(),exit(),isset(),list(),print,unset()

調用類的方法是通過將執行個體化的對象和調用方法的名稱分別作為數組的第0個和第1個參數來進行調用.

調用類的靜態方法可以不用執行個體化該類的對象,而是將類的名稱和靜態方法的名稱分別作為數組的第0個和第1個參數來 進行調用.

在php5.2.3以後,也可以通過使用ClassName::methodName 來調用該類的靜態方法.

除了這些通用的使用者自訂函數,也可以使用匿名函數作為函數參數.


<?php // An example callback functionfunction my_callback_function() {    echo 'hello world!';}// An example callback methodclass MyClass {    static function myCallbackMethod() {        echo 'Hello World!';    }}// Type 1: Simple callbackcall_user_func('my_callback_function'); // Type 2: Static class method callcall_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?> 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.