2017-03-24 34 views
1

您好我試圖在laravel中實現存儲庫模式與抽象類有一些基礎功能,這是我新來的。Laravel抽象類庫

我就行了AbstractRepository->創建($請求)獲得

Parse error:syntax error, unexpected '->' (T_OBJECT_OPERATOR)

; 當我從存儲庫中的抽象調用函數。下面是代碼

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); 
    } 
} 

任何人都可以啓發我嗎?謝謝

+0

可能重複[PHP解析錯誤:語法錯誤,意外的T \ _OBJECT \ _OPERATOR](http://stackoverflow.com/questions/13388541/php-parse-error-syntax-error-unexpected-t-object-運算符) – LuFFy

+0

'PHP 7.0.15-0ubuntu0.16.04.4' – singularity

回答

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!'); 
    } 
} 

看看手冊中的abstractionstatic方法可見手冊。

+0

工作,但現在我得到類型錯誤:參數1傳遞給應用程序\ AbstractsReporistory :: __構造必須是應用程序\摘要\模型的實例,應用程序\模型的實例\在AppServiceProvider中給出的報告。報告模型已經擴展AbstractModel – singularity

+0

AppServiceProvider看起來像這樣:$ app-> bind(ReportRepositoryInterface :: class,function($ app){ }返回新ReportRepository(新報表()); });' – singularity

+0

您可能會發現它更多從長遠來看,這是一個建設性的初始階段,以初學者的方式來學習基本的PHP /編程概念,而不是潛入Laravel項目並解決將會出現的許多問題。話雖如此,你的新問題與命名空間有關。具體來說,尚未爲模型指定合格的路徑,因此它試圖在當前名稱空間中查找該類。有關一般命名空間的更多信息可以在以下網址找到:http://php.net/manual/en/language.namespaces.php – alaric