Reputation: 31
Hi I'm trying to implement the repository pattern in laravel with abstract class to have some base functions which is I'm new at.
I'm getting
Parse error:syntax error, unexpected '->' (T_OBJECT_OPERATOR)
on the line AbstractRepository->create($request); when I call the function from the abstract in the repository. Here are the codes
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);
}
}
Can anyone enlighten me? Thank you
Upvotes: 2
Views: 6199
Reputation: 7679
When a class is abstract
that means that you cannot create an instance of that class directly, not that you can call the methods of the class without creating an instance of the class, that would be a static
method. An abstract
class is meant to be extended. In this case, your ReportRepository
needs to be modified to be:
class ReportRepository extends AbstractRepository implements ReportRepositoryInterface {
public function createReport(array $request){
$this->create($request);
return ApiResponse::responseData($request, 'Record successfully created!');
}
}
Take a look at the manual for abstraction and the manual for static method visibility.
Upvotes: 3