RE: [PHP] OOP .. I just don't get it.
| From: | Martin Towell | Date: | Tue, 05 Mar 2002 22:28:32 +0000 |
| Subject: | RE: [PHP] OOP .. I just don't get it. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-87370@lists.php.net to get a copy of this message | ||
With simple examples, such as that given by Mojo, it would probably be
better to use a normal function. However, once you start getting much more
complex objects, and (possibly) many of them, it's easier to model them
using classes, and not just functions.
One reason to use classes is to abstract the data structure from the caller,
similar to functions abstacting the process from the caller.
eg. I've written a "dataset" class that is basically a two-dimensional
array, but I've added accessor methods that allow (just to name a few):
adding a new row, sorting, pulling back a row by index, pulling data back by
name. It has an internal pointer to the current row so that you can
"rewind", "movenext", etc.
Now think about the last part of this example. To maintain a pointer of
where you are in one of several datasets, you'd need to have a lot of
variables floating around the place that could get over written, misused,
etc. Encapsulating this pointer, and the array, in a class ensures that, if
the class is used properly (ie. no accessing of variables directly, only
through the accessor methods) then there's no need to worry about the
validity of that pointer.
This is but one, more complex, example. There are many more - eg. database,
email, xml - all of which can more easily be represented as objects.
Hope that sways some people into thinking that classes are worth while. I do
agree that there are times functions are better - one must analyse each
problem to determine which is better to use.
Martin
-----Original Message-----
From: Demitrious S. Kelly [mailto:apokalyptik@apokalyptik.com]
Sent: Wednesday, March 06, 2002 7:31 AM
To: 'mojo jojo'; php-general@lists.php.net
Subject: RE: [PHP] OOP .. I just don't get it.
I've often wondered the same thing... which is why I've never moved to
OOP
So I'm patiently waiting for a reply to this message as well :)
-----Original Message-----
From: mojo jojo [mailto:thomasp@chc.quik.co.nz]
Sent: Tuesday, March 05, 2002 12:22 PM
To: php-general@lists.php.net
Subject: [PHP] OOP .. I just don't get it.
Hi
I've been using php for a while now but I have not got my head around
OOP
(classes).
Why bother using them? I've read thru a few tutorials on using classes
and
the examples given are quite simple. This is probably the problem - I
just
can't see the benefit of using this style of programming.
Here is what I'm getting at.
------------USING A CLASS-----------------
class Table {
var $rows;
var $columns;
function MakeTable() {
draw a table with $this->columns as the number of columns
and $this->rows as the number of rows
}
}
$mytable = new Table;
$mytable->rows = 5;
$mytable->columns = 10;
$mytable->MakeTable();
---------------USING A NORMAL FUNCTION-----
function MakeTable($rows,$columns) {
make a table with $rows as the number of rows
and $columns as the number of columns
}
$rows = 5;
$columns = 10;
MakeTable($rows,$columns);
-----------------------------------------------------------
Using a class doesn't appear to give me any benefits - in fact the code
is
longer.
I know that you can spawn more instances of the same class which sounds
useful, however I can also run my function as many times as I like using
different variables.
What am I missing here?
Thanks
Mojo
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php