Eigentlich ist es nicht möglich, in Basis-Klassen auf die Objekt-Eigenschaften einer davon abgeleiteten Klasse zuzugreifen. Ich zeige hier, wie es (ohne Reflection) dennoch möglich ist, durch einen Hack:
Vater-Klasse
Kind-Klasse
Test-Code:
liefert
Damit ist es dann möglich, aus dem Vater die Eigenschaften des Kindes zu manipulieren.
Vater-Klasse
PHP:
<?php
class Vater
{
private $vaterEigenschaft;
/**
* Evil hack to retrieve the properties from the derived class
*
* This method uses a hack to get class properties without the
* use of reflection to minimize the dependencies to php functionality.
*
* First a object dump is retrieved.
* Then we try to find all property names from the object dump by regular
* expression.
* Then we retrieve the object vars from the base class using get_object_class().
* Due to limitation the method get_object_class will return only properties
* of class ORM, which is the base class.
* We walk over all derived class properties to find match in base class. If
* found, we skip that property.
* After all walks we have only properties, which exists in the derived class.
*
* @return array All properties known in the derived class.
*/
public function getObjectProperties()
{
$s = var_export ( $this, true );
$propertyNames = array ();
$matches = array ();
preg_match_all ( '/\'([^\']+?)\' => .*/m', $s, $matches );
if (count ( $matches ) > 1)
{
array_shift ( $matches );
$baseClassProperties = get_object_vars ( $this );
foreach ( $matches [0] as $match )
{
if (! array_key_exists ( $match, $baseClassProperties ))
$propertyNames [] = $match;
}
}
return $propertyNames;
}
/**
* Retrieve a particular property value by calling getter of property
*
* @param string $propertyName
*
* @throws ORMException
* @return mixed
*/
private function getProperty($propertyName)
{
$getter = "get" . ucfirst ( $propertyName );
if (! method_exists ( $this, $getter ))
{
throw new Exception ( "Could not find getter for property $propertyName" );
}
return $this->$getter ();
}
/**
* Set the property using property setter of child class
*
* @param string $propertyName
* The name of the property to set
* @param mixed $value
* The value to set
*
* @throws Exception
*/
private function setProperty($propertyName, $value)
{
$setter = "set" . ucfirst ( $propertyName );
if (! method_exists ( $this, $setter ))
{
throw new Exception ( "Could not find setter for property $propertyName" );
}
$this->$setter ( $value );
}
}
Kind-Klasse
PHP:
class Kind extends Vater
{
private $kindProperty1 = "test";
private $kindProperty2 = 4;
public function getKindProperty1()
{
return $this->kindProperty1;
}
public function setKindProperty1($string)
{
$this->kindProperty1 = $string;
}
public function getKindProperty2()
{
return $this->kindProperty2;
}
public function setKindProperty2($number)
{
$this->kindProperty2 = $number;
}
}
Test-Code:
PHP:
$kind = new Kind ();
var_dump ( $kind->getObjectProperties () );
liefert
Damit ist es dann möglich, aus dem Vater die Eigenschaften des Kindes zu manipulieren.