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?
classes?
Moderator: General Moderators
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Did you catch this thread hob_goblin? It's from a while ago:
http://liberty.dnsprotect.com/~devnetwo ... ht=classes
Mac
http://liberty.dnsprotect.com/~devnetwo ... ht=classes
Mac
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
so if i had
it would print "Hmmm, delicious"??
Code: Select all
<?php
class Fruit {
var $name;
var $color;
var $isEdible = true;
function Fruit($name, $color, $edible=true) {
$this->name = $name;
$this->color = $color;
$this->isEdible = $edible;
}
function eat() {
if ($this->isEdible) { echo "Hmmmm, delicious\n"; }
else { echo "You *must* be kidding\n"; }
}
}
$apple = new Fruit("apple", "red", TRUE);
eat($apple);
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
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.
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.