WhatWhat Posted April 10, 2007 Share Posted April 10, 2007 Hi! Let's say I have 2 classes: User and Database. I have to use the Database class inside the User class. What's the best way to do this? Option #1 - new object class User { __construct() { $this->DB = new Database; } doSomething() { $this->DB->query("SELECT 1"); } } $DB = new Database; $User = new User; $User->doSomething(); Option #2 - reference class User { __construct() { // } doSomething() { $this->DB->query("SELECT 1"); } } $DB = new Database; $User = new User; $User->DB =& $DB; $User->doSomething(); Option #3 - something else? What should I do any why? Thanks Link to comment https://forums.phpfreaks.com/topic/46444-class-inside-a-class/ Share on other sites More sharing options...
Jenk Posted April 10, 2007 Share Posted April 10, 2007 Option 3: <?php class User { private $_db; public function __construct (DB $db) { $this->_db = $db; } public function doSomething() { //do something with $this->_db } } $user = new User(new DB()); ?> Link to comment https://forums.phpfreaks.com/topic/46444-class-inside-a-class/#findComment-225893 Share on other sites More sharing options...
WhatWhat Posted April 10, 2007 Author Share Posted April 10, 2007 Thanks. 2 questions: 1. Why is that better than #1 or #2? 2. What if I want to use X classes in the User class, not just one? Link to comment https://forums.phpfreaks.com/topic/46444-class-inside-a-class/#findComment-225936 Share on other sites More sharing options...
emehrkay Posted April 10, 2007 Share Posted April 10, 2007 I just put together a little set of classes that has a base db class that handles the queries and all of the db interaction and i have classes for each table which contain all of the queries for that table - is that what you're doing? Link to comment https://forums.phpfreaks.com/topic/46444-class-inside-a-class/#findComment-226110 Share on other sites More sharing options...
utexas_pjm Posted April 10, 2007 Share Posted April 10, 2007 1. Why is that better than #1 or #2? Jenk's implementation is better than #1 because you instantiating a DB object every time you instantiate a user a User object. If the DB object is of size x and you have n instances of the User class you are using n*x space. As for #2 It is generally considered bad practice to access member data directly. i.e., <?php //.. $User->DB =& $DB; //.. ?> At the very least you should provide a mutator method such that the member data is not accessed directly. i.e., <?php //.. $User->setDb($db); //.. ?> However, because the User object is composed of a DB object it makes sense to pass this into the constructor as Jenk did. It would not make sense to call doSomething() on the User object if $_db was null. Passing this into the constructor ensures the object is complete before allowing any methods to be called. 2. What if I want to use X classes in the User class, not just one? <?php // .. class User { private $_db; private $_foo; public function __construct (DB $db, $foo) { $this->_db = $db; $this->_foo = $foo; } public function doSomething() { //do something with $this->_db } } //... $user = new User($db, $someOtherInstance); ?> Best, Patrick Link to comment https://forums.phpfreaks.com/topic/46444-class-inside-a-class/#findComment-226113 Share on other sites More sharing options...
Jenk Posted April 11, 2007 Share Posted April 11, 2007 Thanks. 2 questions: 1. Why is that better than #1 or #2? If you want to use a different DB class (but it must have the same interface) it would not be a problem at all, you just provide a different object.. <?php $user1 = new User(new DB()); $user2 = new User(new OtherDB()); ?> 2. What if I want to use X classes in the User class, not just one?add extra arguments, or extra methods for setting the objects, as shown above by Patrick. Link to comment https://forums.phpfreaks.com/topic/46444-class-inside-a-class/#findComment-226445 Share on other sites More sharing options...
WhatWhat Posted April 11, 2007 Author Share Posted April 11, 2007 I just put together a little set of classes that has a base db class that handles the queries and all of the db interaction and i have classes for each table which contain all of the queries for that table - is that what you're doing? I have a DB class that handles the queries, User class (user session, login, logout), some other not-so-important classes and classes for each module of the site, for example "News" - I guess that's similar to "classes for each table". @ Patrick & Jenk Thanks! Link to comment https://forums.phpfreaks.com/topic/46444-class-inside-a-class/#findComment-227026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.