Mailinglisten-Archive |
Hi Ronny,
Ronny Finster schrieb:
> War nur eine Idee mittels StdClass eine Standardobjekt zu erzeugen und
> dieses dann z.B. nach Kunde zu casten.
witzig, dass du das hier ansprichst. Ich habe unser Framework soeben um
ORM Funktionen erweitert ;)
Hier ein paar Ideen, die dir vielleicht weiterhelfen:
Dabei müssen bei mir alle properties, die Spalten darstellen, protected
sein. Vielleicht gibt es bessere Unterscheidungsmöglichkeiten.
abstract class OneMap
{
protected $self;
private $_cols = array();
public function __construct($data = false)
{
$R = new ReflectionObject($this);
$this->self = new ReflectionClass($R->name);
$this->_reflect();
if ($data instanceof stdClass) {
$this->loadProperties($data);
}
}
public function __call($name, $args)
{
// musst du noch implementieren
$val = PFunctions::decamelize(substr($name, 3));
if (!in_array($val, $this->_cols))
return false;
if (substr($name, 0, 3) == 'set') {
$this->$val = $args[0];
return true;
} elseif (substr($name, 0, 3) == 'get') {
return $this->$val;
}
return false;
}
private function _reflect()
{
$className = $this->self->getName();
$properties = $this->self->getDefaultProperties();
foreach ($properties as $propertyName=>$data) {
$Property = new ReflectionProperty($className, $propertyName);
if (!$Property->isDefault() || !$Property->isProtected() ||
$Property->getDeclaringClass()->getName() != $className)
continue;
$this->_cols[] = $Property->getName();
}
}
protected function loadProperties(stdClass $data)
{
foreach ($this->_cols as $col) {
if (!isset($data->$col))
continue;
$method = 'set'.ucfirst(PFunctions::camelize($col));
if (!call_user_method_array($method, &$this,
array($data->$col)))
throw new PException('Could not set property "'.$col.'"');
}
}
}
class Kunde extends OneMap
{
protected $id;
protected $first_name;
public function __construct($data = false)
{
parent::__construct($data);
}
}
$data = new stdClass;
$data->id = 1;
$data->first_name = 'Walther';
$Kunde = new Kunde($data);
echo $Kunde->getId();
echo $Kunde->getFirstName();
php::bar PHP Wiki - Listenarchive