wfdaj
V2EX  ›  PHP

请问,这种情况下返回类型声明有必要吗?

  •  
  •   wfdaj · Jun 22, 2021 · 2600 views
    This topic created in 1803 days ago, the information mentioned may be changed or developed.
    // 文件或目录是否存在
    public static function exists($path): bool
    {
    	return file_exists($path);
    }
    

    这里的 : bool 有必要吗?感谢!

    6 replies    2021-06-28 13:58:04 +08:00
    Trim21
        1
    Trim21  
       Jun 22, 2021 via Android   ❤️ 2
    感觉你这个函数就没有必要…
    simple2025
        2
    simple2025  
       Jun 22, 2021
    @Trim21 可能只是为了函数别名
    Rache1
        3
    Rache1  
       Jun 22, 2021   ❤️ 1
    有必要,虽然现代 IDE 已经很聪明,这种情况即使不声明,也能知道他是返回 bool 。
    linhongye
        4
    linhongye  
       Jun 23, 2021 via Android   ❤️ 1
    何止声明有必要, 函数名还得改成 isExist 让人看到名字都知道这是个 bool
    CodeCodeStudy
        5
    CodeCodeStudy  
       Jun 23, 2021
    有必要,对人的阅读和 IDE 的识别非常有帮助,可以易于排查程序中的错误
    back0893
        6
    back0893  
       Jun 28, 2021
    可以
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2824 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 09:16 · PVG 17:16 · LAX 02:16 · JFK 05:16
    ♥ Do have faith in what you're doing.