I need to cast value in type define in string. For the moment i use this:
/**
* @param mixed $val
* @param string $type
*
* @return bool|float|int|string
*/
protected function castTo($val, $type)
{
switch ($type) {
case 'boolean':
case 'bool':
return (boolean) $val;
case 'integer':
case 'int':
return (int) $val;
case 'string':
return (string) $val;
case 'double':
case 'float':
return (float) $val;
default:
return $val;
}
}
But do you know a better solution ( in php 5.6 and 7+ ) ?