2014-01-20 33 views
-1

我在庫中找到了這段代碼。任何人都可以請幫我這是什麼意思?卡在代碼中

public function __construct(UserRepository $userRepository){ 
    $this->repository = $userRepository; 
} 
+1

能否請您提到的錯誤也是如此。或者你需要什麼幫助? – Roopendra

+0

我沒有得到任何錯誤。我只是不理解構造括號內的東西。我們稱之爲? – user3176531

+1

請參閱[類型提示](http://www.php.net/manual/en/language.oop5.typehinting.php)。 – Prasanth

回答

0

__construct在包含它的class的構造 - 功能這是克當你實例化(創建該類的新對象)時被稱爲 。

(UserRepository $ userRepository)用於這種情況下類型提示參數/參數必須發送給構造,在 ,這意味着你發送給您的構造函數必須是 型UserRepository或至少從繼承的對象UserRepository。

通過傳遞給構造函數的變量的名稱來判斷,我想你會想在Repository Pattern上讀一點 。

來源

http://www.php.net/manual/en/language.oop5.typehinting.php http://www.php.net/manual/en/language.oop5.decon.php

1

的目的是具有這種方法是使用類型UserRepository的另一個目的[1]

public function __construct(UserRepository $userRepository){ 

它保存用於進一步使用是參數initalized。無論你使用一個new Object()new Object($RepositoryObject)你的情況

$this->repository = $userRepository; 

的__construct [2]函數被調用,指令instanziate類到一個變量。

說,構造函數是OOP範式的基礎上,你應該投資一些時間來學習一些關於它(或如何PHP執行空中接力模式[3]),以便與熟練編寫

參考文獻:

[1] http://www.php.net/manual/en/language.oop5.typehinting.php
[2] http://www.php.net/manual/en/language.oop5.decon.php
[3] http://www.php.net/manual/en/language.oop5.php