$P1 = new Player(array(
'name' => 'Player 1',
'strat' => new Strategy(&$this)
));
This is more or less what I’m trying to do. The reference to $this is obviously wrong. What is the best way to accomplish something like this?
Basically, the new Strategy object needs to know what Player it belongs to.
Sure I could do like:
$P1 = new Player();
$P1->strat = new Strategy(&P1);
but that doesn’t seem as concise.
Update:
I guess instead of passing the new strategy object in the array, I would pass the name of the strategy class instead, and then instantiate a new object in the Player constructor.
That will be good enough.
Talk is on.
Okay, if we approach this question from theoretical and conceptual standpoint, we can address the implied question in a couple of different ways. Let’s consider what Strategy to Player is.
As the name implies, Strategy is basically a driver, that could affect the behavior of the play in a particular environment. This further suggests that following approach would make sense:
further, player most likely would be to act in given scope, so, it would use the $strategy object as a controller, and would pass the input from whatever the conditions are met to the strategic behavior controller. That, given the player could either:
1) help on determining the activities to be undertaken
2) use players interface to respond to whatever the action the strategical behavior has to be applied to.
Now, we could assume that there could be different strategies, that could be easily applied to any Player, given that the strategy classes implement the Strategy Interface. In this scenario, Strategy, would not need to know anything about the player. If only, the there are different type of players having different interfaces that would allow for different set of controls. But in that case, it would be wiser to setup extended classes for the particular player classes to implement the extra functionality that those players provide.
Alternatively, there could be centralized Strategy object, which, through configuration interface, based on the type of the player would decided on the proper actions and strategies, both based on the player type and the configuration of its.
but it seems that properly structured strategic support classes would benefit you most.