classes?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

classes?

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Did you catch this thread hob_goblin? It's from a while ago:
http://liberty.dnsprotect.com/~devnetwo ... ht=classes

Mac
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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"??
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you changed

Code: Select all

eat($apple);
to

Code: Select all

$apple->eat();
it would.

Mac
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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)
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Post Reply