本文整理匯總了PHP中XPClass::createGenericType方法的典型用法代碼示例。如果您正苦於以下問題:PHP XPClass::createGenericType方法的具體用法?PHP XPClass::createGenericType怎麽用?PHP XPClass::createGenericType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類XPClass
的用法示例。
在下文中一共展示了XPClass::createGenericType方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: create
function create($spec)
{
if ($spec instanceof Generic) {
return $spec;
}
// Parse type specification: "new " TYPE "()"?
// TYPE:= B "<" ARGS ">"
// ARGS:= TYPE [ "," TYPE [ "," ... ]]
$b = strpos($spec, '<');
$base = substr($spec, 4, $b - 4);
$typeargs = Type::forNames(substr($spec, $b + 1, strrpos($spec, '>') - $b - 1));
// BC check: For classes with __generic field, instanciate without
// invoking the constructor and pass type information. This is done
// so that the constructur can already use generic types.
$class = XPClass::forName(strstr($base, '.') ? $base : xp::nameOf($base));
if ($class->hasField('__generic')) {
$__id = uniqid('', TRUE);
$name = $class->literal();
$instance = unserialize('O:' . strlen($name) . ':"' . $name . '":1:{s:4:"__id";s:' . strlen($__id) . ':"' . $__id . '";}');
foreach ($typeargs as $type) {
$instance->__generic[] = xp::reflect($type->getName());
}
// Call constructor if available
if (method_exists($instance, '__construct')) {
$a = func_get_args();
call_user_func_array(array($instance, '__construct'), array_slice($a, 1));
}
return $instance;
}
// Instantiate, passing the rest of any arguments passed to create()
// BC: Wrap IllegalStateExceptions into IllegalArgumentExceptions
try {
$reflect = new ReflectionClass(XPClass::createGenericType($class, $typeargs));
if ($reflect->hasMethod('__construct')) {
$a = func_get_args();
return $reflect->newInstanceArgs(array_slice($a, 1));
} else {
return $reflect->newInstance();
}
} catch (IllegalStateException $e) {
throw new IllegalArgumentException($e->getMessage());
} catch (ReflectionException $e) {
throw new IllegalAccessException($e->getMessage());
}
}