eddedwards Posted November 26, 2007 Share Posted November 26, 2007 Im wondering what the main differences or limitations there are between the following examples. I think there just 2 ways of doing the same thing but I'm not sure. Should I be using extensions or subclass' or does it even matter. Im currently using the example 1 method and in the same sort of program. <?php // example 1 class DB_Handler { var $subclass, $connection, $result; function __construct($db_type) { switch ($db_type) { case "mysql": $this->subclass = new mysql_database(); break; case "sqlite": $this->subclass = new sqlite_database(); break; } } function query($args) { $this->subclass->query($args); } } $instance = new DB_Handler("sqlite"); $instance->query('foobar'); echo $instance->result; // **************************************************** // example 2 class DB_Handler { var $connection, $result } class mysql_database extends DB_Handler { function query() { // do query stuff } } $instance = new mysql_database(); $instance->query('foobar'); echo $instance->result; ?> Quote Link to comment Share on other sites More sharing options...
448191 Posted November 26, 2007 Share Posted November 26, 2007 Option 1. It's called composite aggregation, composition for short. Do a search on the phrase "favor compostion over inheritance" and you'll eventually find out why option 1 is favourable. Quote Link to comment Share on other sites More sharing options...
eddedwards Posted November 26, 2007 Author Share Posted November 26, 2007 Most of the pages were pretty complicated but I think I can say im gonna stick with the way ive been doing it at least. Cheers for that. Quote Link to comment 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.