2017-03-05 25 views
0
public function addSpouse($name = ($this->data['gender'] == MALE && empty($name)) ? 'Wife' : 'Husband', $suggest = false) 

PHP默認值以下爲上述行:

syntax error, unexpected '$this' (T_VARIABLE)

$this不允許在函數參數?

編輯:功能是一類

+0

當然,不是。 '$ this'是對類實例的引用。如果沒有對象,你打算提及什麼? – abcdn

+0

可能重複的[解析錯誤:語法錯誤,意外(T \ _VARIABLE)](http://stackoverflow.com/questions/28952930/parse-error-syntax-error-unexpected-t-variable) – Anil

+0

addSpouse是在一個類。 – Piyush

回答

0

內它是從內部本身引用類的一個實例的方式,相同的許多其它面向對象的語言。 $this指當前類的實例

參考PHP docs

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

0

不能使用一個變量($這個 - >數據或任何變量)作爲默認的參數值。 默認的功能參數值必須是常數。

public function addSpouse($name = 'Wife') 

是正確的,但不能:

public function addSpouse($name = $data)