NYCPHP Meetup

NYPHP.org

[nycphp-talk] Object Oriented Question

evan heller evan.heller at alum.rpi.edu
Fri Jun 7 15:20:37 EDT 2002


ophir-

I tend to make everything I do now into classes
partly because I'm sick and tired of reinventing
the wheel everytime I make a new site.

Anyhow, for example, I'm building the site:
http://www.comicinfosource.com and everything is a
class.

So for example I have a basic database class that 
knows how to handle a mysql, odbc or a oracle
database
and returns all results as an 2 dimensional
associative array
but when using it all I have to say is such: 
$db = New DB($username,$database,$databasetype);
$db->Connect();
$somesql = "select username,password from users";
$results = $db->Select($somesql);
if (is_array($results)) {
	for ($i=0;$i<count($results);$i++) {
		print $results[$i]["username"];
	}
}
$db->Close();

Now I know that's more code than just going to the
database,
however I can now feed that database object to
other objects
or better yet, not have to worry about
transactions,deletes,inserts
or whatever depending on the database types, as
the database class
 will handle all of that, plus its easier for
others to work with
if they would like to help add to your site.

On my site I also have bookreviews which is really
a class called
Bookreviews (a collection of books) with a
subclass called Book 
(which is a generic book class) which has a
subclass called Database.
As you go higher up the class chain, it becomes
more and more 
abstracted. So the final page may only access a
book, or a 
collection of books based on a search or a
category. It becomes
very flexible and the best part is I can easily
reuse it
later on in another program with minimal or (if
its well done) no
modification at all.

A good example is that a friend of mine was able
to adapt my book
review class system to his site with no changes at
all but the types
of books and the database were different 
(from comics books to real books).

-Evan


--
Evan Heller     
evan.heller at alum.rpi.edu



More information about the talk mailing list