On Fri, 2003-07-18 at 18:19, Andu wrote:
> 
> Say I have a database class with all kinds of functions for connection, 
> data manipulation, errors, etc:
> 
> $db = new db_sql;
> $db->connect();
> $db->do_this();
> $db->do_that();
> 
> How would that be different from an include file with a bunch of functions 
> doing the same thing?
> Is there any performance benefit one way or the other?
> I used EZ_Sql which is cool but didn't seem to speed things up in 
> comparison to the said include file.
> Still don't see the beef.
> 

Using your above example, the above would keep all of the internal data
that keeps track of the resource ID (for the connection), the current
result set, and the current row, etc. within the object instance scope.
Thus everytime you invoke a method on the object it works with it's own
set of data. In all honesty you can do this with functions also, but
then you would have something like the following:

$dbId = db_connect();
$data = db_do_this( $dbId );
$moreData = db_do_that( $dbId, $data )

As you can see this quickly can become cumbersome becaus eof the lack of
encapsulation. However, the OOP paradigm goes even further. Lets imagine
you now want log all use of your functions (methods :). To do this in
OOP you just extend the class and override the methods while still
allowing invocation of the inheritted methods. Thus your usage of your
new object code would look something like follows:

$db = new custom_db_sql;
$db->connect();
$db->do_this();
$db->do_that();

Whereas the function code would resemble the following:

$dbId = custom_db_connect();
$data = custom_db_do_this( $dbId );
$moreData = custom_db_do_that( $dbId, $data )

As you can see updating the function based code required more work. It
wasn't as modular since it couldn't take advantage of inheritance. This
is a trivial example btw, it's not perfect since there are books and
books written on this topic and I'm obviously glossing over a lot of
details. Once you get into design patterns you'll also see other
advantages of using objects versus functions. I hope this helps a teeny
weeny bit.

Cheers,
Rob.
-- 
.---------------------------------------------.
| Worlds of Carnage - http://www.wocmud.org   |
:---------------------------------------------:
| Come visit a world of myth and legend where |
| fantastical creatures come to life and the  |
| stuff of nightmares grasp for your soul.    |
`---------------------------------------------'

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

Reply via email to