1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| class A
{
private $attr_a_1;
public $b_list;
public function __construct($id, $b_id)
{
$this->attr_a_1 = $id;
$this->b_list = array();
foreach ($b_id as $k => $v)
$this->b_list[$k] = new B($v, $this);
}
public function random_func($id)
{
return $this->attr_a_1 - $id;
}
/* .. */
}
class B
{
protected $attr_b_1, $attr_b_2;
public function __construct($id_b, $a_obj)
{
$this->attr_b_1 = $id_b;
$this->attr_b_2 = $a_obj->random_func($id_b);
}
public function change($param, $a_obj)
{
echo "changement de l'objet B : " , $this->attr_b_2 , ' en : "';
$this->attr_b_2 = $a_obj->random_func($param);
echo $this->attr_b_2 , '"', "\n";
}
}
$a = new A(5, array(1, 2, 3));
foreach ($a->b_list as $k => $v)
{
$a->b_list[$k]->change(10, clone $a);
} |
Partager