I'm not an OOP master, but I'll give it a shot.  In your example below
you don't gain much from using OOP.  However, consider this example (all
pseudo code)...

class animal (
        function eat() ...
        function sleep() ...
        function walk() ...
);

class bird extends animal (
        function fly()...
        #inherits eat,sleep,walk
);


Now where this gets useful is when you want to create a class "duck".  You
can just do this:

class duck extends bird (
        function swim()...
        #inherits eat,sleep,walk,fly
);

What's nice about this is you don't need to know the implementation of the
animal or bird class to do this. Heck you don't even need the source (just
the compiled library).  All you need to know is the public functions (or
methods).  However you still get all the functionality of an animal and
bird.

You also get some overhead, but that's the price you pay.

Does that help?

Search the net for OOP tutorial and I'm sure you'll find some better
examples and reasons.

-philip

On Wed, 6 Mar 2002, mojo jojo wrote:

> 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

Reply via email to