为了账号安全,请及时绑定邮箱和手机立即绑定

如何正确扩展和使用其他接口?

如何正确扩展和使用其他接口?

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 关注
  • 178 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号