Re: Speaking of OOP
| From: | Rasmus Lerdorf | Date: | Tue, 09 Jan 2001 22:34:41 +0000 |
| Subject: | Re: Speaking of OOP | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-33569@lists.php.net to get a copy of this message | ||
It isn't inherently obvious to us old-timers who grew up doing everything
in a procedural manner. The main thing a class gives you is
encapsulation. Meaning, you can group a bunch of simple functions
together along with a set of variables. You can almost think of it as a
data structure with user-definable transformation functions.
For example:
class wallet {
var $total;
function wallet($initial_amount) {
$this->total = $initial_amount;
}
function add($amount) {
$this->total += $amount;
}
function display() {
echo '$'.number_format($this->total,2);
}
}
This wallet class has one variable (known as a property) and 3 functions
(known as methods).
You can then do:
$w1 = new wallet(10);
$w2 = new wallet(20);
This creates 2 wallet objects. The first starts off with $10 in it and
the second starts with $20.
To add money to the first wallet:
$w1->add(5);
And to display the current amount in the first wallet:
$w1->display();
There is nothing here you couldn't do using straight procedural code, but
from a code organization point of view some will say that this is cleaner.
-Rasmus
On Tue, 9 Jan 2001, John Guynn wrote:
> I am a hardware guy hacking my way through building dynamic web pages using
> PHP and MySQL and I just can't get the concept of OOP. I don't understand
> what a class can offer that I can't do with a function. I've tried to read
> a couple of books about OOP and a chapter about classes and such in a PHP
> book and I just can't grasp the concept.
>
> Can someone explain to me how a class makes my life easier compared to using
> functions.
>
> Thanks in advance,
>
> John Guynn
>
> This email brought to you by RFCs 821 and 1225.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>