當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。