On Sat, Jul 14, 2012 at 12:04 AM, Paul M Foster <paulf@xxxxxxxxxxxxxxxxx> wrote: > On Fri, Jul 13, 2012 at 12:06:22AM -0500, tamouse mailing lists wrote: > >> It's Friday, so... >> >> Yes, it's true, I have just started looking at using PDO instead of >> mysqli -- a bit behind the times... >> >> My question at this stage, is do people tend to extend the PDO class >> for their own use, or encapsulate it in a class (or do most people use >> it mostly in procedural code?) > > I encapsulated it in my own database class. One reason for that is that > the PDO "class" is really two classes, which is fine for low level > drivers but not production code that I have to use daily. A second > reason is that many of the methods are not really necessary in a > production environment. I want to limit the methods my code uses to > interact with the database. Third, I wanted to handle errors and > exceptions with my own methods, more suitable to the code I write. For > example, a fatal error should only, but always, be issued if the > database code is given something illegal to chew on. Anything else, like > a failure to return the expected result, should simply yield some sort > of sentinal value or something. If *my code* ever gives the database > library something illegal to chew on, then it should only be because of > my bad code, not because a user managed to somehow get bad data through. > That is, my code should always catch illegal data before it ever gets to > the database library. Fourth, encapsulation means that I can have the > class itself carry around some of the data that I would normally have to > explicitly pass around among functions. This way, I make that useful > data (like the number of records returned from the last fetch), part of > the "payload" of the class itself. > > Extension is tricky and requires a more subtle and refined knowledge of > the classes involved. Besides, it exposes methods which I would prefer > not be used by my code. Encapsulation limits the methods of the classes > to just what I deem necessary and no more. I can always write new > methods if I need them. > > Paul > > -- > Paul M. Foster > http://noferblatz.com > http://quillandmouse.com I am rather agreeing with both of you. Back in the days I used to poke at Java, one of the caveats was to only inherit from abstract classes, and encapsulate final classes. It does make more sense to me that way. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php