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?
State pattern. PHP missing friend class option.
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
If having namespace also mean that will be possible to declare class private (accessible only by the namespece/package) then yes.d11wtq wrote:I'm not familiar with the pattern, but by the sounds of it PHP needs namespace support for this to work properly.
Otherwise don't see how namespace will prevent someone from tweaking the context class accessing it with namespace::$context->publicMethod()
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Kind of hackish.. but what about:
EDIT: Or just plain interface type hinting without all the fuss, now I come to think of it.
Code: Select all
class A implements friendship
{
public function doSomething($b)
{
$b->doSomethingElse($this);
}
}
class B
{
public function doSomethingElse(friendship $obj)
{
$this->doSomethingWithFriendlyObject($obj);
}
}
$a = new A;
$b = new B;
$a->doSomething($b);