PHP-Code:
<?php
error_reporting(-1);
ini_set('display_errors', TRUE);
Class ListItem
{
protected $Value = null; // property
protected $Container = null; // property
public function __construct($value)
{
$this->setValue($value);
}
public function setContainer(ListBox $value)
{
$this->Container = $value;
}
public function getContainer()
{
return $this->Container;
}
public function setValue($value)
{
$this->Value = $value;
}
public function getValue()
{
return $this->Value;
}
public function test()
{
echo $this->Value.'<br>';
echo "ich stecke im Container ".get_class($this->Container). '<br>';
echo "der Container heißt: ".$this->Container->getname(). '<hr>';
}
}
class ListBox
{
protected $elemente = array();
protected $name ; // property
public function __construct($name)
{
$this->setname($name);
}
public function setname($value)
{
$this->name = $value;
}
public function getname()
{
return $this->name;
}
public function add(ListItem $item)
{
$item->setContainer($this);
$this->elemente[] = $item;
}
public function tuwas()
{
foreach($this->elemente as $element)
$element->test();
}
}
$lb = new ListBox('Das tolle Ding');
$lb->add(new ListItem('rot'));
$lb->add(new ListItem('blau'));
$lb->tuwas();