$data = array("id" => 1, "name" => "Chris", "biography" => "I am am a PHP developer");
foreach($data as $key => $value) {
if(!$class->hasProperty($key)) {
throw new Exception($key." is not a valid property");
}
if(!$class->hasMethod("get".ucfirst($key))) {
throw new Exception($key." is missing a getter");
}
if(!$class->hasMethod("set".ucfirst($key))) {
throw new Exception($key." is missing a setter");
}
// Make a new object to interact with
$object = new Person();
// Get the getter method and invoke it with the value in our data array
$setter = $class->getMethod("set".ucfirst($key));
$ok = $setter->invoke($object, $value);
// Get the setter method and invoke it
$setter = $class->getMethod("get".ucfirst($key));
$objValue = $setter->invoke($object);
// Now compare
if($value == $objValue) {
echo "Getter or Setter has modified the data.\n";
} else {
echo "Getter and Setter does not modify the data.\n";
}
}
有点意思吧。
您可能感兴趣的文章: