State pattern. PHP missing friend class option.
Posted: Wed Jan 10, 2007 1:51 am
Ok...I see some patterns are not really applicable in PHP because there is no `friend class` concept in php as in C++ for example.
In short a class A which is a friend of class B in C++ have access to private methods in class B.
State pattern is one that suffers from this in PHP. One implementation of the pattern suggest that context is passed as an arguament to each state but problem then is state does not really have full control over the context. Of course private methods could become public but problem then is that not only states will have access to guts of the context but outside world as well.
Found simple UML diagram to depict the issue.
setState() there in some cases could be called from within a state....but revealing method as public is a disaster.
What do you think? Someone come with a solution on that issue you don't consider this a problem?
In short a class A which is a friend of class B in C++ have access to private methods in class B.
State pattern is one that suffers from this in PHP. One implementation of the pattern suggest that context is passed as an arguament to each state but problem then is state does not really have full control over the context. Of course private methods could become public but problem then is that not only states will have access to guts of the context but outside world as well.
Found simple UML diagram to depict the issue.
setState() there in some cases could be called from within a state....but revealing method as public is a disaster.
What do you think? Someone come with a solution on that issue you don't consider this a problem?