centenial Posted October 13, 2008 Share Posted October 13, 2008 Hi all, I have a PHP OOP design question. I've done some searching in google, but wasn't able to turn up anything concrete. I'm hoping some experts can point me to the most elegant solution. I have a set of classes. All of my classes "extend" an abstract class called Base. (This class reads a config file, has a basic set of common methods, and sets up needed parameters) Most of my classes need a class called Mysql to perform database operations. Is the best way to access the Mysql class to pass it in the __constructor in each of my child classes, or to instantiate it in my Base class, and assign it to a public property? Or perhaps there is a better way altogether? I'm really looking for the best way to do this, so thanks in advance for your advice! Link to comment https://forums.phpfreaks.com/topic/128133-making-php-classes-play-nicely-with-each-other-oop-pattern-question/ Share on other sites More sharing options...
corbin Posted October 13, 2008 Share Posted October 13, 2008 A singleton or registry is usually how I get my DB object. Link to comment https://forums.phpfreaks.com/topic/128133-making-php-classes-play-nicely-with-each-other-oop-pattern-question/#findComment-663665 Share on other sites More sharing options...
Liquid Fire Posted October 13, 2008 Share Posted October 13, 2008 Personally i prefer the singleton pattern over the registry pattern and all my singleton pattern have a static get instance() method. I never got what the registry pattern provide as a benifit over the singleton pattern. Link to comment https://forums.phpfreaks.com/topic/128133-making-php-classes-play-nicely-with-each-other-oop-pattern-question/#findComment-663880 Share on other sites More sharing options...
keeB Posted October 16, 2008 Share Posted October 16, 2008 I usually have a BaseDao class which contains the connection to my database. All of my Data(base) Access Objects extend from BaseDao which contains methods like: find(), findUnique(). UserDao (an extension of BaseDao) would defined methods like: getUser(User $person) { $this->findUnique($person); } Link to comment https://forums.phpfreaks.com/topic/128133-making-php-classes-play-nicely-with-each-other-oop-pattern-question/#findComment-666851 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.