I helped design and implement and Droid API written in PHP. Though it works just fine, I am always looking to refactor my code.
Is it ok to have objects within other objects that have a common ancestor?
This may be an elementary question, but I can’t seem to find information to support whether or not this is poor practice.
The goal is to have class foo provide common constants and methods to all.
Example:
abstract class foo {
//constants
//common methods to all
}
class bar extends foo{
//represents something
}
class widget extends foo {
//represents something else
}
class controller extends foo{
//controls flow
public function __construct(){
$this->my_bar= new bar();
$this->my_widget= new widget();
}
}
Classes bar and widget may not need to extend foo, but methods within those objects wouldn’t know about the common things that foo knows without sending parameters.
There seems to be too much redundancy going on, just looking for a best-practice.
Creating a framework requires lots and lots of tries that will make you fall and then get back up on your feet.
The most important thing about your question is to make sure you dont fall into the trap that the Liskov substitution principle describres:
What i suggest the most is that you always try to make your class hierarchy as realistic as possible but remember the SOLID principle at all times. For more information on SOLID, look at my blog at:
http://crazycoders.net/2012/03/confoo-2012-make-your-project-solid/
Good luck