当前位置: 首页>>代码示例>>PHP>>正文


PHP XPClass::createGenericType方法代码示例

本文整理汇总了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());
    }
}
开发者ID:Gamepay,项目名称:xp-framework,代码行数:45,代码来源:lang.base.php


注:本文中的XPClass::createGenericType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。