溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php中trait的用法

發(fā)布時(shí)間:2020-07-22 01:28:30 來源:網(wǎng)絡(luò) 閱讀:344 作者:Lee_吉 欄目:web開發(fā)
  1. 代碼:
    <?php
    /*
    * 定義trait:test1
    */
    trait test1{
    public function sayhello(){
        echo 'hello';
    }
    }
    /*
    * 定義trait:test2
    */
    trait test2{
    public function sayworld(){
        echo 'world';
    }
    }
    /*
    * 定義類test,繼承自trait:test1,test2
    */
    class test{
    use test1,test2;
    /*
     * 定義類test的方法:sayhelloworld
     */
    public function sayhelloworld(){
        // 使用trait:test1中的sayhello方法
        $hello = $this->sayhello();
        // 使用trait:test2中的sayworld方法
        $world = $this->sayworld();
        echo $hello.$world;
    }
    }
  2. 測(cè)試:
    // 實(shí)例化類test的對(duì)象objtest
    $objtest = new test();
    // 調(diào)用對(duì)象objtest的sayhelloworld方法
    $objtest->sayhelloworld();
  3. 輸出:
    php中trait的用法

    廣告

    給大家推薦幾個(gè)好用的工具

  4. 時(shí)間校驗(yàn)工具
  5. ip查詢工具
  6. 二維碼生成工具
  7. json格式化工具
  8. 時(shí)間綴-時(shí)間轉(zhuǎn)換工具
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI