- property_exists: documentation ( source)
- trim: documentation ( source)
<?php
$class = new ReflectionClass(Enrollee::class);
$properties = $class->getProperties();
foreach ($properties as $property) {
$name = $property->getName();
$type = (string)$property->getType();
$standard[$name] = $type;
}
$enrollee = new Enrollee();
foreach ($_POST as $key => $value) {
$nameField = $key;
$valueField = $value;
if (!property_exists(Enrollee::class, $nameField)) {
continue;
}
$type = $standard[$nameField];
if ($type === 'string') {
$valueField = strval($valueField);
} elseif ($type === 'int') {
$valueField = intval($valueField);
}
$valueField = trim($valueField);
$enrollee->set($nameField, $valueField);
}