matthewhaworth Posted August 25, 2007 Share Posted August 25, 2007 Do implement db.class twice? Or can an implemented object use resources from the object it's implemented into? Context? Can image.class use functions from db.class once it's implemented into user.class? Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/ Share on other sites More sharing options...
keeB Posted August 26, 2007 Share Posted August 26, 2007 interface defines a template of functions (and in languages other than PHP, return values) UserManagement (Factory) just instantiates User objects. User objects should contain an instance of the Database layer you want to use. <?php class User { private /*(UserDAO)*/ $dao; function User($name) { $this->dao = new UserDAO(); $this->name = $name; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334188 Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Author Share Posted August 26, 2007 interface defines a template of functions (and in languages other than PHP, return values) UserManagement (Factory) just instantiates User objects. User objects should contain an instance of the Database layer you want to use. <?php class User { private /*(UserDAO)*/ $dao; function User($name) { $this->dao = new UserDAO(); $this->name = $name; } } ?> What do you mean by the database layer? I don't get what your code, DAO Data Access Objects, what're they? Is it my database class being passed to my classes instead of implemented? Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334196 Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Author Share Posted August 26, 2007 Also note, that the factory does more than just pass objects, it performs registration and login (it's the login function that passes the object)... it's never initiated. Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334214 Share on other sites More sharing options...
keeB Posted August 26, 2007 Share Posted August 26, 2007 Lets take first the whole idea behind why you take OOP design over a procedural design. The biggest reason is encapsulation and the next is maintenance. A DAO encapsulates the retrieval of data for a certain object or service, to make it function. Take this example: <?php //first a bad example: class User { . . . // all other methods function getSomeData() { $data = mysql_query("select someData from users where username = .........."); //loop through retrieve data, or whatever } } //and a 'better' example class User { . . . // all other methods function getSomeData() { try { return UserDAO->fetchData(); catch (SomeUserException $sue) { // could not get data, do some error handling. return false; } } } This way, UserDAO can switch between Postgres, MySQL, SQLite, Oracle.. who cares, your user object manages just the user data, and you get the data from an object whose sole purpose is to get data. Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334239 Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Author Share Posted August 26, 2007 Lets take first the whole idea behind why you take OOP design over a procedural design. The biggest reason is encapsulation and the next is maintenance. A DAO encapsulates the retrieval of data for a certain object or service, to make it function. Take this example: <?php //first a bad example: class User { . . . // all other methods function getSomeData() { $data = mysql_query("select someData from users where username = .........."); //loop through retrieve data, or whatever } } //and a 'better' example class User { . . . // all other methods function getSomeData() { try { return UserDAO->fetchData(); catch (SomeUserException $sue) { // could not get data, do some error handling. return false; } } } This way, UserDAO can switch between Postgres, MySQL, SQLite, Oracle.. who cares, your user object manages just the user data, and you get the data from an object whose sole purpose is to get data. I think I understand, but you haven't answered my question.. hmm, could you do me a quick diagram of your structure? Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334253 Share on other sites More sharing options...
keeB Posted August 26, 2007 Share Posted August 26, 2007 i'm a little lazy D: i'll think about it Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334280 Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Author Share Posted August 26, 2007 i'm a little lazy D: i'll think about it Pleasssse? lol Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334287 Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Author Share Posted August 26, 2007 Okay, can someone at least explain the data access object in context, unless it WAS my db class you were referring to? Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334675 Share on other sites More sharing options...
matthewhaworth Posted August 26, 2007 Author Share Posted August 26, 2007 http://en.wikipedia.org/wiki/Data_Access_Object Yes it is my database class you were referring to, I can now review my design and pass it an object instead of implementing it. Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334686 Share on other sites More sharing options...
keeB Posted August 26, 2007 Share Posted August 26, 2007 It was Quote Link to comment https://forums.phpfreaks.com/topic/66702-solved-do-implement-dbclass-twice-or-can-an-implemented-object-use-resources-from/#findComment-334687 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.