douchen2011 2019-04-19 03:15
浏览 86

有没有办法让PHP在比较不同类型时产生警告?

I've inherited a large codebase that almost exclusively uses loose comparisons:

$foo = 1
$bar = "1"

if ($foo == $bar)
{
    doThing();
}

In some cases, this has led to bugs.

I know that I could switch all of the == operands to ===, but I'm afraid that that could cause new issues that remain unnoticed until the code gets pushed to the production environment. Obviously that would be very bad.

Is there some global setting that I can enable to make PHP produce warnings when comparing two different types?

  • 写回答

1条回答 默认 最新

  • douliang6563 2019-04-19 03:31
    关注

    No.

    You might want to take a look at strict typing defined in the docs. But it's not exactly what you're looking for, unfortunately.

    Since PHP introduced type declarations, you can make sure your methods are guaranteed to receive the parameter types requested. You may also declare the return type, which will also guarantee that your method returns the right type.

    I am assuming you inherited a legacy codebase using PHP 5. Your only solution here would be to upgrade to PHP 7 (preferably 7.3) and refactor the methods.

    On the other hand, you could also fork PHP source and write the necessary code yourself or even propose this to other PHP devs. But reviewing your proposal will probably take years.

    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误