Click to See Complete Forum and Search --> : Database class... What's the point?


HomoErectus
09-23-2007, 08:58 AM
What is the point creating a database class when all that you are really doing is just renaming PHP's built in functions?

michael879
09-23-2007, 02:05 PM
It has a more logical structure. Its the same reason anyone would use OOP over non-OOP.

HomoErectus
09-23-2007, 02:17 PM
It has a more logical structure.Can you explain that in layman's terms (http://en.wikipedia.org/wiki/Layman)? What is logical about it?

NogDog
09-23-2007, 03:35 PM
OOP produces "loosely coupled" code by using a collection of classes/objects which are each tightly focused on performing very specific tasks. This loose coupling has two main advantages over more tightly coupled procedural programs: (1) it is easier to reuse code in different parts of the application and in separate applications, and (2) changes to the way a specific part of the application works do not cascade throughout the application, making it both easier to modify and easier to debug.

Please note, however, that simply using classes and objects does not mean that the resulting code is object-oriented and loosely coupled. It is entirely possible (if somewhat more difficult) to write object-oriented code without classes, and to write procedural code that does use classes.

If OOP is still pretty new to you, you might want to take a look at this tutorial at sun.com (http://java.sun.com/docs/books/tutorial/java/concepts/) for a high-level overview of the concepts.

bokeh
09-23-2007, 03:54 PM
Using a database class puts another layer between your script and the data.

If today the data is in MySQL and tomorrow it will be in OBDC or sqlite that change can be made just by swapping the file the database class is in for a new one. No changes to any of your scripts would be necessary. (Of course all your queries would have had to have been written in standard SQL.)

rulian
09-24-2007, 12:19 PM
never thought if it like that bokeh, but you do make a very good point!

michael879
09-24-2007, 12:49 PM
well theres ways to do that without using classes bokeh. I learned PHP without classes so I never really bothered to relearn everything with them. What I do is have seperate file with all the mysql information. That way if my database changes I just edit that file.

michael879
09-24-2007, 12:51 PM
also, like nogdog said, the mysql_* functions are pretty much OOP also. The only real advantage you get with the class is possibly that you can pass the objects to functions as 1 parameter (Im not familiar with the class so I dont know if this would be useful or not in the case of mysql), and also that its more readable for anyone used to programming with classes.

ellisgl
09-24-2007, 03:17 PM
Make it's easier to do 2 connections at one time too. (I'm not much of a OOP fan)

bokeh
09-24-2007, 03:17 PM
What I do is have seperate file with all the mysql information.I'm not talking about connect details, I'm talking about an abstraction layer. Anywhere you write mysql_* in your script would need to be modified if you changed to another database. With a database class that would not be true (if it were written properly).

ellisgl
09-24-2007, 03:22 PM
Some of the classes out there can use different db's - and you just change a variable in a configuration file. Of course I've never dealt with anything besides mysql with PHP.

michael879
09-24-2007, 06:33 PM
oo sorry bokeh misread your post. I thought you were talking about changing the physical database, not changing the type of database.

NogDog
09-24-2007, 11:12 PM
If you want an OOP database abstraction layer, don't forget that PHP comes with one already: PDO (http://www.php.net/pdo).

codebudo
09-25-2007, 01:12 AM
PHP comes with several database layers, but you may also want to at least read about Object Relational Mapping (ORM) tools before you decide. Propel (http://propel.phpdb.org/trac/) is the favorite among php developers, but Hibernate is more well known for java development.


---
http://www.codebudo.com/

bokeh
09-25-2007, 03:12 AM
If you want an OOP database abstraction layer, don't forget that PHP comes with one already: PDO (http://www.php.net/pdo).Yeah, but only from 5.1 onwards and it has to be turned on in the build. There are some nice examples of what I was trying to explain on that page though:$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach ($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}Also using a foreach loop feels a lot more natural to PHP minded people.

MrCoder
09-25-2007, 05:36 AM
Custom error handling and logging could also be handled within the class, such as outputting query's to a log file and doing profiling.