jamesmpollard Posted June 6, 2015 Share Posted June 6, 2015 Hi Guy's Now this may be a stupid question but I was wondering if anyone could help me accomplish something. Here's what I'm trying to do (in brief). CLASSONE.PHP class one extends Core { public function __construct() { } public function atestone() { return $this; } public function __destruct() { } } CLASSTWO.PHP class two extends Core { public function __construct() { } public function atesttwo() { return $this; } public function __destruct() { } } CORE.PHP class Core{ public function __construct() { } public function testcore() { return $this->atestone(); // OR return $this->atesttwo(); } public function __destruct() { } } I'm looking to have a core class basically. Ideally, all classes can utilise other functions but my main aim is to have one core class that can use (freely) all other classes that are extended to it. Is this possible? Thanks in advance James Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/ Share on other sites More sharing options...
ginerjm Posted June 6, 2015 Share Posted June 6, 2015 The reason to have a class that extends another class is to add some functionality to the parent class ie, the first class. Your goal of having a class called 'core' that will use all the classes extended from it is IMHO a bit backwards. Yes the 'core' class can be used by all your scripts but the functionality of the child classes that extend it will not be available unless you include them and construct objects of each child class. Then those items will have all the functionality of the specific class they belong to as well as the 'core' class. Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/#findComment-1513363 Share on other sites More sharing options...
jamesmpollard Posted June 6, 2015 Author Share Posted June 6, 2015 The reason to have a class that extends another class is to add some functionality to the parent class ie, the first class. Your goal of having a class called 'core' that will use all the classes extended from it is IMHO a bit backwards. Yes the 'core' class can be used by all your scripts but the functionality of the child classes that extend it will not be available unless you include them and construct objects of each child class. Then those items will have all the functionality of the specific class they belong to as well as the 'core' class. Sorry, I didn't really explain too well. I will be having a bootstrapper file that will include all the classes (class one, class two and so on) and the core. I would then like to use the core to use all other classes. To make life easier, simpler and to have less clutter, ect. Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/#findComment-1513366 Share on other sites More sharing options...
jamesmpollard Posted June 6, 2015 Author Share Posted June 6, 2015 (edited) Maybe a better question would be, what is the best way of using classes in a simple, easy, OOP manner? Bare in mind that many of my classes need to use functions from other classes to save cloning the same functions many times over. Interfaces, abstracts, ect. I'm not fussed, just need as simple way of doing it. EDIT: Forgot to mention, by OOP, I need the classes to be "chained". That would probably solve the problem of using functions from separate classes. Edited June 6, 2015 by jamesmpollard Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/#findComment-1513377 Share on other sites More sharing options...
jcbones Posted June 6, 2015 Share Posted June 6, 2015 (edited) Dependency Injection is the terms you are looking for. class what { function __construct(ever $ever) { $this->ever = $ever; } public function DIme() { return $this->ever->me(); } } class ever { public function me() { return 'Method in class ever.'; } } $ever = new ever(); $what = new what($ever); echo $what->DIme(); Edited June 6, 2015 by jcbones Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/#findComment-1513383 Share on other sites More sharing options...
ignace Posted June 7, 2015 Share Posted June 7, 2015 There is no reason you should have one class that does and knows everything. It's far better to create specialised classes for your pages. It's also far better for performance as you'll quickly be loading too many unneeded classes. Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/#findComment-1513392 Share on other sites More sharing options...
jamesmpollard Posted June 7, 2015 Author Share Posted June 7, 2015 Dependency Injection is the terms you are looking for. class what { function __construct(ever $ever) { $this->ever = $ever; } public function DIme() { return $this->ever->me(); } } class ever { public function me() { return 'Method in class ever.'; } } $ever = new ever(); $what = new what($ever); echo $what->DIme(); Is there a way of making it so that the classes are not all in one file? Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/#findComment-1513404 Share on other sites More sharing options...
jamesmpollard Posted June 7, 2015 Author Share Posted June 7, 2015 There is no reason you should have one class that does and knows everything. It's far better to create specialised classes for your pages. It's also far better for performance as you'll quickly be loading too many unneeded classes. Agreed. A crazy man would make one giant class for the whole site, benchmarking would be a disappointing result lol. Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/#findComment-1513405 Share on other sites More sharing options...
jcbones Posted June 7, 2015 Share Posted June 7, 2015 Of course you don't have to put all classes in one file. That is what include is for. Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/#findComment-1513418 Share on other sites More sharing options...
ginerjm Posted June 7, 2015 Share Posted June 7, 2015 As long as you realize that if 'core' is your master class and 'one' and 'two' are classes that extend 'core', you can't use just 'core' and access the methods that classes 'one' or 'two' define. You made it sound like that in your reply to my post. Quote Link to comment https://forums.phpfreaks.com/topic/296684-php-class-sharing/#findComment-1513420 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.