Dânêl Posted October 4, 2009 Share Posted October 4, 2009 Hi, I'm writing some personal library also to do practice with OOP... In particular I'm working on a user class which uses database and I want to do it simply reusable in different environments For example I want to do it database type and database class independent. Is it possible? I will try to explain this. There could be already defined a Database class inside a project. How could I do to use it or mine database class without troubles? I thought to use some class properties as functions (like C function pointer) and editing them with constructor allowing for example, the use of mysql_query instead of mysqli_query or "my_mysql_query" . Is this a good way or not? An abstraction layer could be better but I have to know how are defined third part db classes, right? PS: I know the existence of PDO but it can be disabled by the hosting provider (I know some hoster doing this...) Thanks for any hints Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/ Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 Wrapper pattern deals with that. In general it's a class that adapts interfaces of two classes that have to interact together. Unfortunately you need to create a separate wrapper for each pair. Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-930080 Share on other sites More sharing options...
marvelade Posted October 4, 2009 Share Posted October 4, 2009 PS: I know the existence of PDO but it can be disabled by the hosting provider (I know some hoster doing this...) There are also hosters that keep register_globals On, but that generally means that their service sucks. Find one hosting partner that u are comfortable with (preferrably *not* a free one); they should have enough PHP expertise to realise what decent settings are instead of basing their settings on some blog post saying that PDO is not implemented well in PHP4 (while they ru PHP5) and that register_globals is a handy tool for developers... Most hosting companies know a lot about Linux/apache but nothing about PHP, unfortunately. greetz, Marv Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-930081 Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 Never really worked with OOP in PHP, but from what I remember from using them in C# back at uni shouldn't a single interface do the job? Ie. you have an interface 'Database' that must include function query(), function num_rows(), etc, etc. You'd then have classes 'DB_MsSQL', 'DB_MySQL' etc that implents the 'Database' interface. Then within the 'User' class you would have a private member of type 'Database' called $db (or whatever you wish). The 'User' class would then have functions that would use $this->db to call database functions. If the person employing the class 'User' wishes to change to a different type of database they would merely have to change the declaration of $db to $db = new DB_MsSQL(); rather than $db = new DB_MysSQL(); Does it not work like this in PHP, or did I miss the point completely (quite possible)? Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-930082 Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 Cags: Yup. Your DB_MsSQL and DB_MySQL classes would be in fact wrappers for database drivers. Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-930090 Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 Ahh right, yes, I see what your saying now. Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-930099 Share on other sites More sharing options...
Dânêl Posted October 4, 2009 Author Share Posted October 4, 2009 Thanks to all for your replies. There are also hosters that keep register_globals On, but that generally means that their service sucks. Find one hosting partner that u are comfortable with (preferrably *not* a free one); they should have enough PHP expertise to realise what decent settings are instead of basing their settings on some blog post saying that PDO is not implemented well in PHP4 (while they ru PHP5) and that register_globals is a handy tool for developers... Most hosting companies know a lot about Linux/apache but nothing about PHP, unfortunately. greetz, Marv I agreed with you, but unfortunately usually I write little script for people who had already hosted their sites and for this I know hosting with PDO, SOAP disabled. I wrote also a gallery in PHP and then discovered that the hoster disabled GD. As a result I had to disable checks and Image resizing. It's a mystery for me the reason why well paid hosting service have these limitations... As regards the answers to my questions. SO what I need is something like this? <?php interface db_wrapper { //Execute a query public function query($query); // Other functions } class Db implements db_wrapper { private $dbclass; public function __construct() { $this->dbclass = new TheClassYouLike(); } public function query($query) { $this->dbclass->execute($query); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-930215 Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 Looks like a handbook example Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-930232 Share on other sites More sharing options...
Dânêl Posted October 6, 2009 Author Share Posted October 6, 2009 Really ? thanks Is it a good idea to use a function for every different sql istruction (select,update,delete,insert) ? I see them studying some DB classes and abstraction layers taken from OpenSource project and frameworks. I think this way makes a bit hard the use of nested queries (like a Select inside a Select) or JOINs: it requires too much code even if it make possible to use non standard, db specific, sql query or change query only once for (maybe possible?) future SQL commands update. Isn't it? What is better? A general or a specific query function? Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-931435 Share on other sites More sharing options...
Mchl Posted October 7, 2009 Share Posted October 7, 2009 Different database use different SQL dialects. What works on MySQL can throw a syntax error on Postgres and vice versa. In general, if you're using an abstraction layer, you should refrain from using raw SQL. This means some limitations (and also less performance, since more data handling has to be done in PHP). Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-932160 Share on other sites More sharing options...
Dânêl Posted October 7, 2009 Author Share Posted October 7, 2009 I love it when there are no followed standards.... It is quite impossible to have a truly db abstraction layer. I have to think much about to do as much as possible without make it unreadable or too big and less-sense Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-932205 Share on other sites More sharing options...
448191 Posted November 7, 2009 Share Posted November 7, 2009 I love it when there are no followed standards.... It is quite impossible to have a truly db abstraction layer. How so? It is quite possible to generate queries that will work using different SQL flavours. The key is though to NOT use any straight SQL. One interface is used to do the CRUD operations, different driver adapters for different RDBMS flavours will generate different SQL. It is not rocket science, just a LOT of work. PS: I know the existence of PDO but it can be disabled by the hosting provider (I know some hoster doing this...) What I would recommend is creating a wrapper around PDO, creating a separate adapter for MySQLi. Indeed some stupid hosts have MySQLi enabled but not PDO. But the two are similar enough to create a wrapper around both to get a unified interface. I would use the PDO interface as the base for it. I recently had to create a wrapper around mysqli for Doctrine (the current version really only supports PDO, but they do use a wrapper and a separated interface, which made it possible to create a wrapper around MySQLi that basically implemented the PDO methods that Doctrine uses, but with MySQLi as the backend. Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-953062 Share on other sites More sharing options...
flash gordon Posted November 11, 2009 Share Posted November 11, 2009 I just posted this a minute ago, but java's JDBC is completely awesome as an database abstraction layer. http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html The record sets and execute statements are pretty nice. Quote Link to comment https://forums.phpfreaks.com/topic/176447-what-is-the-best-way-with-oop-and-object-interaction/#findComment-955459 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.