The following code is available on Page 159 of "Deep PHP object-oriented, patterns, and practices. Here is a demonstration of the combination mode. The array_udiff function is used in the removeUnit method. The author intends to remove the $ unit object from the $ units attribute. I tried to find that it didn't work. The key is to have the following code on Page 159 of "Deep PHP object-oriented, patterns and practices.
Here is a demonstration of the combination mode. The array_udiff function is used in the removeUnit method. The author intends to remove the $ unit object from the $ units attribute. I tried to find that it does not work. The key is to store objects in the $ units parameter. Objects cannot be sorted during comparison, so they fail. Is there a good way to exclude an object from the array?
class Army extends Unit{
private $units = array();function addUnit(Unit $unit){ if(in_array($unit, $this->units, true)){ return; } $this->units[] = $unit;} function removeUnit(Unit $unit){ $this->units = array_udiff($this->units, array($unit), function ($a, $b) { return ($a === $b) ? 0: 1;}); } function bombardStrength(){ $ret = 0; foreach($this->units as $unit){ $ret += $unit->bombardStrength(); } return $ret;}
}
$ Army = new Army;
$archer = new Archer();//$archer->addUnit(new Archer);$army->addUnit($archer);$army->addUnit($archer);$army->addUnit(new Archer);$army->addUnit(new LaserCannonUnit);
$army2 = new Army();$army2->addUnit(new Archer);$army2->addUnit($archer);$army2->addUnit($archer);$army2->removeUnit($archer);$army->addUnit($army2);$army->addUnit($army2);print_r($army);echo $army->bombardStrength()."\n";$army->removeUnit($army2);$army->removeUnit($army2);$army->removeUnit($army2);$army->removeUnit($archer);echo $army->bombardStrength();print_r($army);
Reply content:
The following code is available on Page 159 of "Deep PHP object-oriented, patterns, and practices.
Here is a demonstration of the combination mode. The array_udiff function is used in the removeUnit method. The author intends to remove the $ unit object from the $ units attribute. I tried to find that it does not work. The key is to store objects in the $ units parameter. Objects cannot be sorted during comparison, so they fail. Is there a good way to exclude an object from the array?
class Army extends Unit{
private $units = array();function addUnit(Unit $unit){ if(in_array($unit, $this->units, true)){ return; } $this->units[] = $unit;} function removeUnit(Unit $unit){ $this->units = array_udiff($this->units, array($unit), function ($a, $b) { return ($a === $b) ? 0: 1;}); } function bombardStrength(){ $ret = 0; foreach($this->units as $unit){ $ret += $unit->bombardStrength(); } return $ret;}
}
$ Army = new Army;
$archer = new Archer();//$archer->addUnit(new Archer);$army->addUnit($archer);$army->addUnit($archer);$army->addUnit(new Archer);$army->addUnit(new LaserCannonUnit);
$army2 = new Army();$army2->addUnit(new Archer);$army2->addUnit($archer);$army2->addUnit($archer);$army2->removeUnit($archer);$army->addUnit($army2);$army->addUnit($army2);print_r($army);echo $army->bombardStrength()."\n";$army->removeUnit($army2);$army->removeUnit($army2);$army->removeUnit($army2);$army->removeUnit($archer);echo $army->bombardStrength();print_r($army);
The direct loop and unset are solved.
foreach($this->units as $k => $u){ if($u === $unit){ unset($this->units[$k]); } }