Dat Posted April 1, 2010 Share Posted April 1, 2010 How do i use a class inside of another class? I have class blogPost and class database. How do i use database inside class blogPost class blogPost { include ('database'); $db = new database; } Link to comment https://forums.phpfreaks.com/topic/197202-how-do-include-a-class-inside-of-a-class/ Share on other sites More sharing options...
trq Posted April 1, 2010 Share Posted April 1, 2010 class blogPost { private $_db; public function __construct($db) { $this->_db = $db; } } include ('database'); $blog = new blogPost(new database); Link to comment https://forums.phpfreaks.com/topic/197202-how-do-include-a-class-inside-of-a-class/#findComment-1035103 Share on other sites More sharing options...
Dat Posted April 1, 2010 Author Share Posted April 1, 2010 class blogPost { private $_db; public function __construct($db) { $this->_db = $db; } } include ('database'); $blog = new blogPost(new database); What if i already have a __construct? Link to comment https://forums.phpfreaks.com/topic/197202-how-do-include-a-class-inside-of-a-class/#findComment-1035108 Share on other sites More sharing options...
Axeia Posted April 1, 2010 Share Posted April 1, 2010 Then pass the parameter to do that to the construct or create a method public function setDatabase($db) { $this->_db = $db; } Although if $this->_db is vital for the blogPost class (which I suspect it is) it belongs in the construct. As a few hopefully constructive tips.. tip #1 PHP(5) supports type hinting so this works as well: __construct(database $db) The more strict you can get the better imo. (Als provides more useful errors when you pass in a parameter of the wrong type) tip #2 You may want to stick to the general PHP (and java and probably other programming languages as well) code convention to start classnames with a capital letter, so Database and BlogPost instead of database/blogPost. (this is gonna be a rather late reply.. left this open in a tab for half a day) Link to comment https://forums.phpfreaks.com/topic/197202-how-do-include-a-class-inside-of-a-class/#findComment-1035366 Share on other sites More sharing options...
trq Posted April 1, 2010 Share Posted April 1, 2010 What if i already have a __construct? Define another argument to it. Link to comment https://forums.phpfreaks.com/topic/197202-how-do-include-a-class-inside-of-a-class/#findComment-1035533 Share on other sites More sharing options...
jcbones Posted April 1, 2010 Share Posted April 1, 2010 include ('database'); class blogPost { function __construc() { $this->db = new database(); } } Link to comment https://forums.phpfreaks.com/topic/197202-how-do-include-a-class-inside-of-a-class/#findComment-1035543 Share on other sites More sharing options...
trq Posted April 2, 2010 Share Posted April 2, 2010 include ('database'); class blogPost { function __construc() { $this->db = new database(); } } That's not such a good idea, because now the blogPost object is tied to a specific database object. Link to comment https://forums.phpfreaks.com/topic/197202-how-do-include-a-class-inside-of-a-class/#findComment-1035584 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.