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; ?> Link to comment https://forums.phpfreaks.com/topic/78953-solved-sub-classes-or-extensions/ 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. Link to comment https://forums.phpfreaks.com/topic/78953-solved-sub-classes-or-extensions/#findComment-399615 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. Link to comment https://forums.phpfreaks.com/topic/78953-solved-sub-classes-or-extensions/#findComment-399674 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.