2012-11-21 85 views
0

這是一個超級基本問題,但我的班級沒有按照我期望的方式工作。我是第二年的CS學生,所以對我來說很容易!我唯一知道的其他語言是Java。這裏是我的課,叫做class.Team.php(代表團隊)。簡單的班級方法不起作用

<?php 
class Team 
{ 
    private $teamId=''; 
    private $teamName='Tyler'; 
    private $teamCity=''; 
    private $homeField=''; 
    private $headCoach=''; 
    private $mascot=''; 
    private $wins=0; 
    private $losses=0; 
    private $roster; 

    function display(){ 
    return $teamName; 
    } 
} 
?> 

所以,當我調用display()方法應該返回一個字符串(硬編碼爲「泰勒)。這是我的測試腳本,它只是顯示什麼。一個空白頁。我缺少的東西? ?

<?php 
include ('class.Team.php'); 
$um = new Team; 
$string = $um->display(); 
print ($string); 
?> 

THANKS!

+0

你能解決你的身份嗎? – PeeHaa

+1

也是'$ this-> teamName'。另外的手冊:http://www.php.net/manual/en/language.oop5.basic.php – PeeHaa

+0

沒有標籤在主題:http://meta.stackexchange.com/questions/19190/should-questions-include -tag-in-titles –

回答

5

您需要使用$this關鍵字。

function display(){ 
    return $this->teamName; 
} 
+1

好的。男人,我一直爲Tylers生根多年...... –

1

這不是Java。您必須使用$this來引用自己的實例。

return $this->teamName;