If I have a class named Machine, and a class named Widget, how can I prevent the Widget class from being instantiated unless it is performed by a method in the Machine class?
I know that nested classes are not valid, but this code exemplifies the functionality I'm looking to achieve:
Code: Select all
class Machine{
private class Widget{
//Widget properties and methods
}
public function makeWidget(){
$newWidget = new Widget();
return $newWidget;
}
//Machine properties and methods
}
$myMachine = new Machine();
$myWidget = $myMachine->makeWidget(); //would work fine
$myOtherWidget = new Widget(); //would fail