簡體   English   中英

確定FQCN是類,接口還是特征的最佳方法

[英]Best way to determine whether a FQCN is a class, interface, or trait

我試圖確定FQCN是類,特征還是接口。 這就是我目前在想的,但是有人有更好的主意嗎?

/**
 * @return string|null Returns the type the FQCN represents, returns null on failure
 */
function fqcnType(string $fqcn) : ?string
{
    if (interface_exists($fqcn) === true) {
        return 'interface';
    } elseif (class_exists($fqcn) === true) {
        return 'class';
    } elseif (trait_exists($fqcn) === true) {
        return 'trait';
    } elseif (function_exists($fqcn) === true) {
        return 'function';
    }

    return null;
}

function fqcn_exists(string $fqcn) : bool
{
    return fqcnType($fqcn) !== null;
}
/**
 * @return string|null Returns the type the FQCN represents, returns null on failure
 */
function fqcnType(string $fqcn) : ?string
{
    $types = [
        'interface',
        'class',
        'trait',
        'function',
    ];

    foreach($types as $type) {
        if(true === ($type.'_exists')($fqcn)) {
            return $type;
        }
    }

    return null;
}

function fqcn_exists(string $fqcn) : bool
{
    return null !== fqcnType($fqcn);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM