【问题标题】:How to use class_exists correctly in PHP 5.4如何在 PHP 5.4 中正确使用 class_exists
【发布时间】:2020-07-01 18:43:40
【问题描述】:

我正在使用无法升级的带有 PHP 5.4 的旧系统。我必须通过添加一个名为 FPDF/FPDI 的 PDF 文件生成库来做一个小改动:

protected function getPdfParserInstance(StreamReader $streamReader)
{
    /** @noinspection PhpUndefinedClassInspection */
    if (\class_exists(FpdiPdfParser::class)) {
        /** @noinspection PhpUndefinedClassInspection */
        return new FpdiPdfParser($streamReader);
    }

    return new PdfParser($streamReader);
}

问题在于 ::class 是在 PHP 5.5 中添加的,正如 in this question 所解释的那样。

问题是:需要对该函数进行哪些更改才能在 PHP 5.4 中工作?

【问题讨论】:

    标签: php class php-5.4 fpdi


    【解决方案1】:

    ::class 只计算包含类全名的字符串,因此请改用它。查看use 语句的top of the file,您会发现FpdiPdfParser 只是一个别名,因此您的代码应重写为如下所示:

    if (class_exists("setasign\\FpdiPdfParser\\PdfParser\\PdfParser"))
    

    严格来说,您不需要转义反斜杠;很少有escape sequences 包含大写字母,但最好还是这样做。

    这不太可能是您遇到的唯一兼容性问题。如果一个系统可以运行 PHP 5.4,那么它几乎肯定可以运行 PHP 5.6,而 PHP 5.6 已经过时了。

    【讨论】:

    • 问题是我无法升级(“其他人”必须升级,“其他人”不想要)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 2017-07-02
    • 2016-07-14
    • 2021-12-11
    • 2013-10-13
    相关资源
    最近更新 更多