久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 PHP 中將實(shí)例方法作為參數(shù)傳遞

Passing an instance method as argument in PHP(在 PHP 中將實(shí)例方法作為參數(shù)傳遞)
本文介紹了在 PHP 中將實(shí)例方法作為參數(shù)傳遞的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我想創(chuàng)建一個(gè)監(jiān)聽器類

class Listener {
    var $listeners = array();
    
    public function add(callable $function) {
        $this->listeners[] = $function;
    }

    public function fire() {
        foreach($this->listeners as $function) {
            call_user_func($function);
        }
    }
}

class Foo {
    public function __construct($listener) {
        $listener->add($this->bar);
    }
    
    public function bar() {
        echo 'bar';
    }
}



$listener = new Listener();
$foo = new Foo($listener);

但是此代碼失敗并出現(xiàn)此錯(cuò)誤:

But this code fails with this error:

注意:未定義的屬性:18 行 index.php 中的 Foo::$bar

Notice: Undefined property: Foo::$bar in index.php on line 18

可捕獲的致命錯(cuò)誤:傳遞給 Listener::add() 的參數(shù) 1 必須是可調(diào)用的,給定 null,在第 18 行在 index.php 中調(diào)用并在第 5 行定義了 index.php

Catchable fatal error: Argument 1 passed to Listener::add() must be callable, null given, called in index.php on line 18 and defined index.php on line 5

我做錯(cuò)了什么?

推薦答案

  • 在 PHP 5.4 之前,沒有名為 callable 的類型,所以如果將它用作類型提示,則表示名為 callable 的類".如果您使用 PHP >= 5.4,callable 是一個(gè)有效的提示.

    • Before PHP 5.4, there was no type named callable, so if you use it as a type hint, it means "the class named callable". If you use PHP >= 5.4, callable is a valid hint.

      可調(diào)用對(duì)象由描述可調(diào)用對(duì)象名稱的字符串(例如函數(shù)名或類方法名)或數(shù)組指定,其中第一個(gè)元素是對(duì)象的實(shí)例,第二個(gè)元素是要調(diào)用的方法的名稱.

      A callable is specified by a string describing the name of the callable (a function name or a class method name for example) or an array where the first element is an instance of an object and the second element is the name of the method to be called.

      對(duì)于 PHP <5.4、替換

      For PHP < 5.4, replace

      public function add(callable $function)
      

      與:

      public function add($function)
      

      調(diào)用它:

      $listener->add(array($this, 'bar'));
      

      這篇關(guān)于在 PHP 中將實(shí)例方法作為參數(shù)傳遞的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

      【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗(yàn)證問題中添加自定義注冊(cè)字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡(jiǎn)單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 亚洲第一天堂 | 中文字幕视频三区 | 一区二区国产精品 | 亚洲色图插插插 | 一级黄色毛片免费 | www午夜视频 | 四虎影院在线播放 | 国产精品精品视频一区二区三区 | 欧美一区二区在线观看 | 黑人久久 | 中文一区二区 | 久久精品这里 | 男人的天堂一级片 | 男女羞羞免费网站 | 中文字幕日韩一区 | 毛片一级片 | 日本成人在线播放 | 国产色婷婷久久99精品91 | 成av在线| 天天搞天天操 | 国产精品高清在线 | 欧洲妇女成人淫片aaa视频 | 在线看日韩 | 都市激情亚洲 | 99reav| 久久精品国产久精国产 | 91精品国产欧美一区二区成人 | 国产玖玖 | 干干干操操操 | 偷拍亚洲色图 | 911影院 | 福利在线观看 | 国产精品视频一区二区三区不卡 | 亚州综合一区 | 精品国产乱码久久久久久丨区2区 | 国产免费观看视频 | 久久久www成人免费精品张筱雨 | 国产毛片久久久久久久久春天 | 欧美日韩在线一区 | 精品欧美一区二区三区久久久 | 国产精品久久久亚洲 |