本文整理汇总了PHP中Reflection::getPrototypes方法的典型用法代码示例。如果您正苦于以下问题:PHP Reflection::getPrototypes方法的具体用法?PHP Reflection::getPrototypes怎么用?PHP Reflection::getPrototypes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Reflection
的用法示例。
在下文中一共展示了Reflection::getPrototypes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _castParameters
/**
* Cast parameters
*
* Takes the provided parameters from the request, and attempts to cast them
* to objects, if the prototype defines any as explicit object types
*
* @param Reflection $reflectionMethod
* @param array $params
* @return array
*/
protected function _castParameters($reflectionMethod, array $params)
{
$prototypes = $reflectionMethod->getPrototypes();
$nonObjectTypes = array('null', 'mixed', 'void', 'unknown', 'bool', 'boolean', 'number', 'int', 'integer', 'double', 'float', 'string', 'array', 'object', 'stdclass');
$types = array();
foreach ($prototypes as $prototype) {
foreach ($prototype->getParameters() as $parameter) {
$type = $parameter->getType();
if (in_array(strtolower($type), $nonObjectTypes)) {
continue;
}
$position = $parameter->getPosition();
$types[$position] = $type;
}
}
if (empty($types)) {
return $params;
}
foreach ($params as $position => $value) {
if (!isset($types[$position])) {
// No specific type to cast to? done
continue;
}
$type = $types[$position];
if (!class_exists($type)) {
// Not a class, apparently. done
continue;
}
if ($value instanceof $type) {
// Already of the right type? done
continue;
}
if (!is_array($value) && !is_object($value)) {
// Can't cast scalars to objects easily; done
continue;
}
// Create instance, and loop through value to set
$object = new $type();
foreach ($value as $property => $defined) {
$object->{$property} = $defined;
}
$params[$position] = $object;
}
return $params;
}