dongmu1989 2015-06-06 12:20 采纳率: 0%
浏览 87

PHP和Composer:运行时检查类是否可自动加载

Recently I decided to upgrade some projects from my own (...) autoloader solution to a Composer PSR-4 autoloader. My code already followed PSR-4 so, no big deal there.

On a specific case, I had the following code:

public static function isAutoLoadable($className)
{

    $className = ltrim($className, "\\");
    $file = $GLOBALS["Path"] . "/src/" . str_replace("\\", "/", $className) . ".php";
    if (false !== stream_resolve_include_path($file))
        return $file;

    return false;

}

It allowed me to check whatever a given class name could be loaded, without actually trying to load it and result on a PHP Class * not found error.

Use case: I'm currently using it to replace controllers that by some reason couldn't be found with a generic error one, avoiding an App crash and just telling the user something went wrong... then extra internal logging is done.

A function would be good since I can call it before trying to load a controller... I don't want this behavior to spread to any other classes on the app

So my question is: Is there a equivalent way to check if Composer is able to autoload some class without forcing it to load it and cause an error? -- So I can take further actions in case the class wasn't found?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测