Mailinglisten-Archive |
Moin,
da ich etwas die Zeit im Nacken habe, habe ich es erstmal so gelöst:
class foo {
private $bar = Bar;
private $id = null;
private $blub = array();
public function __construct($data) {
if ( ! is_array($data) || count( $data ) != 3) {
throw new Exception("wrong paramater count in class " . __CLASS__);
}
$defaultProperties = $this->getClassDefaultProperties();
foreach ($data as $name => $value) {
if ( array_key_exists( $name, $defaultProperties ) ) {
if ( ! is_array( $defaultProperties[$name] ) &&
$defaultProperties[$name] != null ) {
if ( ! $value instanceof $defaultProperties[$name] ) {
throw new Exception($name . " expected instance of " .
$defaultProperties[$name] . " in class " . __CLASS__ . " but " . $value
. " given");
}
}
$this->$name = $value;
}
}
}
protected function getClassDefaultProperties() {
$tmp = array();
$self = new ReflectionClass( $this );
foreach ( $self->getDefaultProperties() as $key => $value ) {
$tmp[$key] = $value;
}
return $tmp;
}
public function getId() {
return $this->id;
}
}
class Bar {
public function __construct() {
}
}
$test['bar'] = new Bar();
$test['id'] = 12;
$test['blub'] = array(0 => "Hallo", 1 => "Guten Tag");
$bar = new Bar();
$foo = new foo( $test );
print_r($foo);
So kann ich zumindest kontrollieren, ob die richtige Anzahl an
Attributen übergeben wurde und ich kann zusätzlich die Abhängigkeiten
auflösen.
Is zwar quick & dirty, aber funktioniert. ;-)
Gruß,
Ronny
php::bar PHP Wiki - Listenarchive