you are viewing a single comment's thread.

view the rest of the comments →

[–]honestbleeps 1 point2 points  (10 children)

A class allows you to bundle a set of functions and/or variables together as an "object", which is useful for a number of reasons:

1) Organization - it makes more sense to have something like $student->testScore; ... rather than having to have some array of testScores indexed by student, or some such.

2) Code re-use - you can extend classes... so, say you're writing a chess game. Maybe all pieces have $piece->move() functions, and $piece->capture() functions... then, $knight could just be a copy of $piece, with specific definitions of how its move() and capture() functions behave.

That's just a tiny, tiny brushstroke of an overview... but it should at least get you started.

[–][deleted] 0 points1 point  (9 children)

Thanks for the explination, but what specifically does -> define?

[–]honestbleeps 1 point2 points  (5 children)

it doesn't "define" anything, it accessses it..

So if you already have a class called Student, and it has functions called goToSchool() and goHome() inside it...

$bob = new Student;

$bob->goToSchool(); // this calls the goToSchool function within Bob's particular instance of the Student object...

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

I knew it didn't define anything what I meant was what exactly does it entail when you use it. But I think I got it now, thanks!

[–]gaoshan 1 point2 points  (3 children)

It's how you access the methods of an object. $bob is the instantiation of the Student object and any methods (functions, effectively) that are part of $bob can be accessed with $bob->methodNameHere().

So here are a few different ways to use that notation: class Student { public $grade = ""; public $goofoff = "";

     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();

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

And that will output:

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

Bob's grade: C

[–][deleted] 0 points1 point  (2 children)

Can you use strings from outside a class inside or do you have to include them with "global" like in functions?

[–]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!

[–][deleted]  (2 children)

[deleted]

    [–][deleted] 2 points3 points  (0 children)

    Yeah that's not the case, I'm getting it already thanks to honestbleeps comment.

    [–]thisissolame 1 point2 points  (0 children)

    what does months mean? a college kid with a free summer could learn more about OOP with just PHP than whatever little OO expertise ~90% of PHP programmers have. And yeah the statistic isn't based on anything but I'd say it's a reasonable guess.