0

this is a super basic question but my classes are not working the way I expect them to. I am a CS student in my second year so go easy on me! The only other language I know is Java. Here is my class, called class.Team.php (represents a team).

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

So when I call the display() method it should return a string (hardcoded as "tyler). Here is my test script. It is simply displaying nothing. A blank white page. Am I missing something??

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

THANKS!

10
  • Could you fix your identation? Commented Nov 21, 2012 at 20:47
  • 1
    Also it is $this->teamName. Also the manual: php.net/manual/en/language.oop5.basic.php Commented Nov 21, 2012 at 20:48
  • no tags in subject: meta.stackexchange.com/questions/19190/… Commented Nov 21, 2012 at 20:49
  • @MichaelBerkowski 2 space indentation? eeeew ;) Commented Nov 21, 2012 at 20:50
  • btw: class functions are called 'methods', while class variables are called 'members' Commented Nov 21, 2012 at 20:51

2 Answers 2

5

You need to use the $this keyword.

function display(){
    return $this->teamName;
}
Sign up to request clarification or add additional context in comments.

Comments

1

It's not Java. You have to use $this to reference own instance.

return $this->teamName;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.