2016-09-30 55 views
-2

我正在tryng解決這個上codewars網站:PHP:Codewars註冊運動

更正此代碼,以便迎接函數返回的預期值。

class Person { 
    public $name; 
    public function __construct($name) { 
    $this->name = $name; 
    } 
    public function greet($guest) { 
    return 'Hello $guest, my name is $name'; 
    } 

} 

哪裏出錯?

+2

如果不能解決至少應註冊的運動,也許你應該先研究一下你選擇的編程語言,練習一下,然後再回到codewars。 – croxy

回答

0

Complex (curly) syntax只能用雙引號""

改變它像這樣:

class Person { 
    public $name; 
    public function __construct($name) { 
    $this->name = $name; 
    } 
    public function greet($guest) { 
    return "Hello $guest, my name is {$this->name}"; 
    } 

} 

輸出:

$x = new Person('john'); 
echo $x->greet('peter'); 

//Hello peter, my name is john 
+0

對不起,網站上的正確行是:return'你好$客人,我的名字是$ name'; – sunn

+0

我剛測試過它,並通過了測試。 – Neat

+0

謝謝! @整齊 – sunn