I'm trying to create a singleton factory class, that instantiates objects that cannot otherwise be instantiated. In other words, the Widgets extend from the Factory class, and have a protected __construct(). The problem is that my Widgets are obviously then inheriting many Factory specific methods I do not want them to possess, such as the creation methods themselves.
Here's an example:
Code: Select all
abstract class Factory{
protected static $params = array(); //these are parameters that would be used by widgets at runtime, but are set by and stored in the Factory class
public static function createWidget($args){
return new Widget($args);
}
public static function setParam($args1, $args2){ //should also be invisible to the Widget class, as Widgets shouldn't be able to set params, only read them
self::$params[$args1] = $args2;
}
}
class Widget extends Factory{
protected function __construct($args){
//do constructor stuff
}
}
$myFailFactory = new Factory(); //fails of course, as it should
$myFailWidget = new Widget("foo"); //fails like it should as well
$myGoodWidget = Factory::createWidget("bar"); //success
Code: Select all
$myFailWidget = Widget::createWidget("Yikes"); //succeeds of course, but I don't want it to!
$myFailWidget->setParam("foo", "bar");