Page 1 of 1

classes?

Posted: Tue Jul 23, 2002 12:26 am
by hob_goblin
I still dont quite grasp classes... why use them?

Wouldn't it be just as easy to put all of your self-created functions in a file, and include it?

Posted: Tue Jul 23, 2002 2:15 am
by twigletmac
Did you catch this thread hob_goblin? It's from a while ago:
http://liberty.dnsprotect.com/~devnetwo ... ht=classes

Mac

Posted: Tue Jul 23, 2002 2:40 am
by hob_goblin
so if i had

Code: Select all

<?php
class Fruit &#123;
    var $name;
    var $color;
    var $isEdible = true;

    function Fruit($name, $color, $edible=true) &#123;
        $this->name = $name;
        $this->color = $color;
        $this->isEdible = $edible;
    &#125;

    function eat() &#123;
        if ($this->isEdible) &#123; echo "Hmmmm, delicious\n"; &#125;
        else &#123; echo "You *must* be kidding\n"; &#125;
    &#125;    
&#125;




$apple = new Fruit("apple", "red", TRUE);
eat($apple);

?>
it would print "Hmmm, delicious"??

Posted: Tue Jul 23, 2002 3:11 am
by twigletmac
If you changed

Code: Select all

eat($apple);
to

Code: Select all

$apple->eat();
it would.

Mac

Posted: Tue Jul 23, 2002 11:11 am
by hob_goblin
Ah, now i see why people would use it, for XML - and news scripts and such.. but for most of what I do, classes would just slow things down, because I wouldnt have many instances of the functions.. (I read some of those previous posts)

Posted: Tue Jul 23, 2002 11:52 am
by llimllib
When you use classes, you group similar bits of data and the functions to manipulate them. This means that a person who wants to use the functionality of a class doesn't need to know how it was implemented. All they need to know is what each function does, and they can use it. This allows a programmer to add and remove objects (classes) easily.

By (ideally) eliminating the need to understand implementation, classes greatly ease development with many developers and allow programs to use a much more elegant design.

For a practical example, imagine that you used the Pear::DB class a year ago. After that, somebody figured out a radically new way to implement what's inside that class 20 times faster. This person totally changed the variables inside the class, but left the purpose of the functions to be the same. Now, today, a year later, you have the new class, and you can use it in *exactly* the same way as you did before, despite the fact that the variables inside the class are entirely different.

This is just a basic statement on OOP (the process of using classes). Tutorials and information about it are all over - I advise that you read about it, because almost every program of any size nowadays incorporates it.