Laravel Abstract Class Repository

Привет, я пытаюсь реализовать шаблон хранилища в laravel с абстрактным классом, чтобы иметь некоторые базовые функции, в которых я новичок.

я собираюсь

Ошибка разбора: синтаксическая ошибка, неожиданное ‘->’ (T_OBJECT_OPERATOR)

в строке AbstractRepository-> create ($ request);
когда я вызываю функцию из реферата в хранилище. Вот коды

AbstractRepository.php

    <?php

namespace App\Abstracts;

use App\Exceptions\NoResourceFoundException;
use App\Exceptions\ResourceNotFoundException;

abstract class AbstractRepository
{

/**
* @var Model
*/
protected $model;

/**
* @var array
*/
public $errors = [];

/**
* @param Model $model
*/
public function __construct(Model $model)
{
$this->model = $model;
}

public function create(array $data)
{
return $this->model->create($data);
}
}

ReportRepositoryInterface.php

<?php

namespace App\Interfaces;

interface ReportRepositoryInterface {

public function createReport (array $data);
}
?>

ReportRepository.php

<?php

namespace App\Repositories;

use App\Interfaces\ReportRepositoryInterface;
use App\Models\Report;
use App\Services\ApiResponse;
use Illuminate\Http\Request;
use App\Abstracts\AbstractRepository;

class ReportRepository implements ReportRepositoryInterface {

public function createReport(array $request){
AbstractRepository->create($request);
return ApiResponse::responseData($request, 'Record successfully created!');
}
}

?>

ReportsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Interfaces\ReportRepositoryInterface;
use Illuminate\Support\Facades\Log;

class ReportsController extends Controller
{

public function __construct(ReportRepositoryInterface $report, Request $request)
{
$this->report = $report;
$this->request = $request;
}

public function createReport()
{
$data = $this->request->all();
return $this->report->createReport($data);
}
}

Кто-нибудь может просветить меня? Спасибо

1

Решение

Когда класс abstract это означает, что вы не можете создать экземпляр этого класса напрямую, а не то, что вы можете вызывать методы класса, не создавая экземпляр класса, это было бы static метод. abstract класс должен быть расширен. В этом случае ваш ReportRepository должен быть изменен, чтобы быть:

class ReportRepository extends AbstractRepository implements ReportRepositoryInterface {

public function createReport(array $request){
$this->create($request);
return ApiResponse::responseData($request, 'Record successfully created!');
}
}

Взгляните на руководство для абстракция и руководство для статический видимость метода.

2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector