亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確擴展和使用其他接口?

如何正確擴展和使用其他接口?

PHP
揚帆大魚 2022-07-16 09:51:42
我正在嘗試為所有其他接口使用基本接口,如下所示:基礎接口<?phpnamespace App\Repositories\Data;interface IDataRepository{    public function getAll();    public function getById($id);    public function create($model);    public function update($model);    public function delete($id);}實現的基礎接口    <?php namespace App\Repositories\Data;    use Illuminate\Database\Eloquent\Model;    class DataRepository implements IDataRepository    {        // model property on class instances        protected $model;        // Constructor to bind model to repo        public function __construct(Model $model)        {            $this->model = $model;        }        // Get all instances of model        public function getAll()        {            return $this->model->all();        }        // create a new record in the database        public function create($model)        {            return $this->model->create($model);        }        // update record in the database        public function update($model)        {            $record = $this->find($model.id);            return $record->update($model);        }        // remove record from the database        public function delete($id)        {            return $this->model->destroy($id);        }        // show the record with the given id        public function getById($id)        {            return $this->model-findOrFail($id);        }    }我試圖使用基本接口的接口<?phpnamespace App\Repositories;use App\Repositories\Data\IDataRepository;interface ITestRepository extends IDataRepository{}執行<?php namespace App\Repositories;use App\Library\Classes\Test;use Illuminate\Database\Eloquent\Model;class TestRepository implements ITestRepository{}在我的控制器中,我試圖只調用測試存儲庫,以便我可以使用所有基本存儲庫功能:但我收到以下錯誤:類 App\Repositories\TestRepository 包含 5 個抽象方法,因此必須聲明為抽象方法或實現其余方法如果我只使用我的基本接口并通過模型,我的應用程序就可以正常工作。在我的所有其他接口之間共享基本接口中的功能以防止代碼重復的正確方法是什么?我很感激任何幫助。
查看完整描述

3 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

我認為包含接口聲明的所有方法的Trait是最佳選擇。類似的東西(不確定邏輯):


namespace App\Repositories;


trait TDataRepository

{

    // model property on class instances

    protected $model;


    // Constructor to bind model to repo

    public function __construct(Model $model)

    {

        $this->model = $model;

    }


    // Get all instances of model

    public function getAll()

    {

        return $this->model->all();

    }


    // create a new record in the database

    public function create($model)

    {

        return $this->model->create($model);

    }


    // update record in the database

    public function update($model)

    {

        $record = $this->find($model.id);

        return $record->update($model);

    }


    // remove record from the database

    public function delete($id)

    {

        return $this->model->destroy($id);

    }


    // show the record with the given id

    public function getById($id)

    {

        return $this->model-findOrFail($id);

    }

}

然后將其用于具有基本接口的類:


namespace App\Repositories;


use App\Library\Classes\Test;

use Illuminate\Database\Eloquent\Model;


class TestRepository implements ITestRepository

{

    use TDataRepository;

}


查看完整回答
反對 回復 2022-07-16
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

<?php



namespace App\Repositories;



use App\Interfaces\ITestRepository;


class TestRepository implements ITestRepository

{


public function getAll()

{

    // TODO: Implement getAll() method.

}


public function getById($id)

{

    // TODO: Implement getById() method.

}


public function create($model)

{

    // TODO: Implement create() method.

}


public function update($model)

{

    // TODO: Implement update() method.

}


public function delete($id)

{

    // TODO: Implement delete() method.

}

}


類必須聲明為抽象或實現方法'getAll'、'getById'、'update'、'create'、'delete' 所以默認情況下,所有方法都是接口中的抽象方法,你必須在這個類中定義所有方法。


查看完整回答
反對 回復 2022-07-16
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

該類TestRepository不應實現任何接口,而應擴展DataRepository:


<?php namespace App\Repositories;


use App\Repositories\Data\DataRepository;


class TestRepository extends DataRepository

{

}

DataRepository已經包含接口的實現IDataRepository。當您創建一個實現類時,ITestRepository您必須定義接口中所有方法的實現(在您的情況下與基本接口相同)。


查看完整回答
反對 回復 2022-07-16
  • 3 回答
  • 0 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號