PHP 5.4中如何正确使用class_exists
How to use class_exists correctly in PHP 5.4
我使用的是 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 中运行?
::class
只是评估包含 class' 全名的字符串,因此请改用它。查看 use
语句的 top of the file ,您会发现 FpdiPdfParser
只是一个别名,因此您的代码应该重写为如下所示:
if (class_exists("setasign\FpdiPdfParser\PdfParser\PdfParser"))
严格来说,您不需要转义反斜杠;少数 escape sequences 包含大写字母,但无论如何这样做是个好习惯。
这不太可能是您遇到的唯一兼容性问题。如果一个系统可以 运行 PHP 5.4,它几乎可以肯定 运行 PHP 5.6 已经过时了几年。
我使用的是 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 中运行?
::class
只是评估包含 class' 全名的字符串,因此请改用它。查看 use
语句的 top of the file ,您会发现 FpdiPdfParser
只是一个别名,因此您的代码应该重写为如下所示:
if (class_exists("setasign\FpdiPdfParser\PdfParser\PdfParser"))
严格来说,您不需要转义反斜杠;少数 escape sequences 包含大写字母,但无论如何这样做是个好习惯。
这不太可能是您遇到的唯一兼容性问题。如果一个系统可以 运行 PHP 5.4,它几乎可以肯定 运行 PHP 5.6 已经过时了几年。