colshrapnel / thebestpdowrapper

World's simplest PDO wrapper
35 stars 8 forks source link

Why is this wrapper inappropriate for OOP and what is an alternative? #9

Open alex996 opened 8 years ago

alex996 commented 8 years ago

The author mentions that this wrapper class would be a bad practice in an OO design. I assume that this is because of the nature of the singleton pattern, which seems to be extremely criticized; but could this be more elaborated?

I'd been implementing a simple ORM with this wrapper class for DB operations. It worked well, and I was satisfied with it until I've stumbled upon heavy critique of singleton and PDO wrappers in general. Most argue that PDO is a sufficient layer by itself and should not be extended or wrapped. However, in the case of ORM (e.g. Eloquent or Doctrine), what is a proper design approach to handle DB operations? Why would singleton be inappropriate if we introduce models? What other techniques be advised? And lastly, is it possible, at all, to have a similarly concise and straightforward class that would be appropriate for an ORM and OOP?

lcherone commented 6 years ago

Its wrapping PDO in a static class.. which offers nothing except a static method.. nuff said.. not sure why it even has best in its name.. you just need to look at the code (static, mysql only dsn!, no namespacing or composer support, no tests, bad formatting) so I suspect its just typical click-bait SEO tactics...

A better alternative would be: https://github.com/phppackage/pdo-wrapper though PDO does not need a "wrapper" of any sort. Use models with dependency injection.

joshcangit commented 5 years ago

There doesn't seem to be a way to use it in another function, either in the same class or in another class that extends from this, without using DB:: which makes it no different than using it in the main code outside of functions and classes.

I suppose why @colshrapnel considers it the best PDO wrapper is because he could make it static with method chaining. I like method chaining, even though I don't know how to implement it yet, but I don't think that could justify as the best.

Not saying I could do better. It's just that the wrapper can't be used in certain ways. It looks really beginner friendly though since the usage is the same inside or outside of functions.

I made some PHP wrappers but not without help from his website. https://github.com/joshcangit/flexphpwrapper/tree/oop/pdo