[PHP] Re: OOP .. I just don't get it.

2002-03-05 Thread Philip Hallstrom

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://wwwphpnet/)
 To unsubscribe, visit: http://wwwphpnet/unsubphp



-- 
PHP General Mailing List (http://wwwphpnet/)
To unsubscribe, visit: http://wwwphpnet/unsubphp




[PHP] Re: OOP .. I just don't get it.

2002-03-05 Thread mojo jojo

Hi Phillip

Yes, thanks this does help.

The problem with the tutorials that I have seen so far is that they are very
simplistic and therefore do not reveal the benefits of this style of
programming.

If anybody knows of any good tutorials, I would appreciate a link.

Thanks

Peter (Mojo)


Philip Hallstrom [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED];
 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




Re: [PHP] Re: OOP .. I just don't get it.

2002-03-05 Thread Erik Price


On Tuesday, March 5, 2002, at 05:40  PM, mojo jojo wrote:

 Hi Phillip

 Yes, thanks this does help.

 The problem with the tutorials that I have seen so far is that they are 
 very
 simplistic and therefore do not reveal the benefits of this style of
 programming.

 If anybody knows of any good tutorials, I would appreciate a link.

FWIU, part of the problem is that, while PHP can simulate the way OOP 
works, it's not truly object oriented.  For instance, in PHP you can 
only extend a class once (I might be wrong but I heard that somewhere).  
FWIU, a language like Python or Java is able to do quite a bit more with 
objects, and Python and Java both are very well-suited to larger 
applications (rather than the typical Perl or PHP script) which can 
benefit better from having these classes available for defining objects.

I'm still trying to wrap my head around it myself.  To date, the only 
way I've seen objects successfully used in PHP is in Larry Ullman's PHP 
Advanced for the WWW (Visual QuickPro Guide, PeachPit Press).  It's 
used to separate the HTML page used in a PHP script from the PHP code 
itself, and it works very very well.  There are probably hundreds of 
other applications in PHP that make effective use of objects, but I 
haven't seen them.

Just yesterday I spent four hours trying to create a class that could 
help me manage the numerous forms that are used on the site I am 
developing.  Unfortunately, my complete inexperience with this resulted 
in frustration and I gave up, going back to functions.  I'm also not 
very intelligent, and working with classes and objects requires the 
programmer to be able to abstract their data in a way that I haven't yet 
learned to do.  Sigh... someday.


Erik




Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] RE: OOP .. I just don't get it.

2002-03-05 Thread Andrew Chase

Well, the 'Table' class is definitely of limited usefulness..

but where classes come in especially handy is when you want to build
reusable code for handling common tasks, like, for instance, MySQL
connections and queries... so that your PHP code looks something like

//

$db = new mysqlObject;

$db-connect(localhost,username,password,mydatabase);
$db-doQuery(select * from mytable);
foreach($db-resultRows){
//Do stuff with the query results here
}
$db-disconnect();

/ instead of something like: /

$dbconnection = mysql_connect(localhost,username,password);

mysql_select_db(mydatabase,$dbconnection);
$result = mysql_query(select * from mytable,$dbconnection);
$resultrows = array();
while($row = mysql_fetch_array($result)){
$resultrows[] = $row;
}
foreach($resultrows as $temprow){
//Do stuff with the query results here
}
mysql_disconnect($dbconnection);

//

The first, most obvious advantage is that the code becomes a easier to read,
which is important if you're working on a complex project.

You could arguably create a set of regular functions for MySQL stuff if you
really wanted to, but you'll have to keep careful track of any global
variables you might be using;  When you're working with a class all of your
class variables and functions are properties of an object, always with the
same name and easily accessed, but separate from any local variables you're
working with.  When you're working with a collection of functions, your
variables are all over the place and prone to inconsistency.

For instance, handling two different database connections in the same script
is easy with a class; using the hypothetical example from above, you know
that the results of your query are always going to be stored in the
'resultRows' property of the object, so it's as easy as accessing
$foo-resultRows and $bar-resultRows.

If you were using a collection of functions, you would have to come up with
a second variable name to store results from the second connection for that
particular script, and even if you try to maintain consistency among all the
scripts on your site using your collection of database functions, chances
are that things will get messy at some point; it's easier to keep track of
commonly used variable and function names at the class level.

Hmm, I hope that sort of made sense... it's a tough thing to explain.  It
took me a long time to get my head around the usefulness of OOP, too- as you
said, examples are usually not very practical.  The first explanation of OOP
that I ever read dealt with a hypothetical 'vehicle' class, and although I
understood the *theory*, I wondered for the longest time why I would want to
write a program for describing different kinds of imaginary cars and turning
them on and off, or extend it to describe imaginary boats and motorcycles.
;)

-Andy


 -Original Message-
 From: mojo jojo [mailto:[EMAIL PROTECTED]]

 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.

:: table class example snipped ::

 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?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php