Boo-urns Posted February 9, 2009 Share Posted February 9, 2009 I am thinking of having a few classes for different sections / events for a site. First off I have extended mysqli class. Now where I am confused is I have a class for validation (passwords, zipcodes...etc) Is it best to have a db connection there as well or is there a better way of doing that? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/ Share on other sites More sharing options...
flyhoney Posted February 9, 2009 Share Posted February 9, 2009 I dont think its too unreasonable for the Validation class to create its own Database() object Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/#findComment-758437 Share on other sites More sharing options...
trq Posted February 9, 2009 Share Posted February 9, 2009 Why would a validation class need a database connection? Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/#findComment-758449 Share on other sites More sharing options...
Boo-urns Posted February 9, 2009 Author Share Posted February 9, 2009 Validate user logins, or should that be under a User class? What do you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/#findComment-758451 Share on other sites More sharing options...
genericnumber1 Posted February 9, 2009 Share Posted February 9, 2009 One simple possible consideration you may have is to implement a database interaction object as a singleton and retrieve the object when it is needed. Unfortunately that also leads to a global connection that can be fairly limiting when implementing your other classes. What I prefer to do is instead of using a singleton or, as suggested before, creating a new database object inside of the class, is to use dependency injection and pass the class a data object when the class is instantiated (edit: or when specific methods are invoked). This allows for more flexible testing of the class and more flexible types of data sources without having to manipulate global data and further interfering with the tested object's encapsulation. Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/#findComment-758456 Share on other sites More sharing options...
premiso Posted February 9, 2009 Share Posted February 9, 2009 One simple possible consideration you may have is to implement a database interaction object as a singleton and retrieve the object when it is needed. Unfortunately that also leads to a global connection that can be fairly limiting when implementing your other classes. What I prefer to do is instead of using a singleton or, as suggested before, creating a new database object inside of the class, is to use dependency injection and pass the class a data object when the class is instantiated (edit: or when specific methods are invoked). This allows for more flexible testing of the class and more flexible types of data sources without having to manipulate global data and further interfering with the tested object's encapsulation. I am not sure of this in PHP5, but I had that setup in my scripts in PHP4 and it was amazing at how much slower the script ran by passing in the objects. I actually had a "Main" method where I would instantiate an object/return it if called, so only one instance of the object was created, but for whatever reason it slowed it down a ton. Since, this was for PHP4, I created functions, it would still only instantiate the class on a first call, but allowed for that to be used throughout any class, and it worked well. As far as this on PHP5, I have no clue of the side-effects. Just thought I would share my PHP4 experience with that scenario of including the class with the constructor or parameter. Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/#findComment-758497 Share on other sites More sharing options...
genericnumber1 Posted February 9, 2009 Share Posted February 9, 2009 With PHP4 objects were passed by value by default, you had to specifically tell them to pass by reference. So sorry if this is a stupid question, but were you being sure to pass them by reference? Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/#findComment-758530 Share on other sites More sharing options...
Boo-urns Posted February 9, 2009 Author Share Posted February 9, 2009 genericnumber1 - can you somewhat describe dependency injection? I've found an example but I'm having problems creating it to work. I'm getting an error: "Fatal error: Call to undefined method BookReader::getChapers()" Which i imagine is the DI not working. <?php interface Db { public function executeSql($sql); } class MySqlDb implements Db { public function __construct($username, $password, $host) { // i took out the constructor and used the mysqli constructor } public function executeSql($sql) { // return something here nothing new... } } class BookReader { private $_db; public function __construct(Db $db) { $this->_db = $db; } public function getChapters() { return $this->_db->executeSql('SELECT name FROM chapter'); } } $book = new BookReader(new MySqlDb(DB_USER, DB_PASS, DB_HOST)); $chapters = $book->getChapers(); Instead of using an interface i just extended the mysqli class for MySqlDb. I just can't wrap my head around this yet :-/ Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/#findComment-758558 Share on other sites More sharing options...
genericnumber1 Posted February 10, 2009 Share Posted February 10, 2009 getChapters() you missed a 't' Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/#findComment-758567 Share on other sites More sharing options...
Boo-urns Posted February 11, 2009 Author Share Posted February 11, 2009 Wow what an embarrassment :-/ thanks for spotting it Quote Link to comment https://forums.phpfreaks.com/topic/144534-solved-oop-organization/#findComment-759725 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.