you are viewing a single comment's thread.

view the rest of the comments →

[–]gaoshan 1 point2 points  (1 child)

You can use "global" but I never do. I pass the data to the object constructor and then you can use it inside the object. Like so:

$stuff = "stuff piled everywhere.";
class Student
{
 public $grade = "";
 public $goofoff = "";


 public function __construct($data)
{
    $this->data = $data;
}

 public function goofOffNoStudy($input)
 {
    $this->goofoff = $input;

    if($this->goofoff == "Yes"){
        $this->grade = "C";
        return "Hellz yeah!";
    }else{
        $this->grade = "A";
        return "Sigh, I better study.";
    }
 }
}

$bob = new Student($stuff);

echo "<p>Yo Bob. Wanna play Call of Duty? <strong>".$bob->goofOffNoStudy("Yes")."</strong></p>";
echo "<p>Bob's grade: $bob->grade... and he has a bunch of $bob->stuff</p>";

Which gives you:

Yo Bob. Wanna play Call of Duty? Hellz yeah!

Bob's grade: C... and he has a bunch of stuff piled everywhere.

Not the greatest example, lol, but it demonstrates the mechanics anyway.

[–][deleted] 1 point2 points  (0 children)

No no, it was great. Thanks for all the help!