V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
wfdaj
V2EX  ›  PHP

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

  •  
  •   wfdaj · Jun 22, 2021 · 2520 views
    This topic created in 1771 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   ·   5615 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 08:23 · PVG 16:23 · LAX 01:23 · JFK 04:23
    ♥ Do have faith in what you're doing.