You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让PHP7解析器强制要求所有函数必须设置返回类型?

强制PHP函数必须定义返回类型的解决方案

Great question! Let's get straight to the point: PHP (as of version 8.3) does NOT include a built-in declare directive like declare(strict_types=1); that forces the interpreter to throw a parse error when a function lacks a return type declaration—even if strict_types is enabled, PHP will happily execute code with functions missing return types.

That said, you absolutely can enforce this rule across your entire project or codebase—you just need to use static analysis tools (the industry standard for this kind of strict code enforcement) instead of relying on PHP's core interpreter. Here's how to do it:

Tools like PHPStan or Psalm are designed to catch these kinds of issues before your code even runs. They can be configured to throw errors if any function/method doesn't have an explicit return type.

Example with PHPStan:

  1. Install PHPStan via Composer:
    composer require --dev phpstan/phpstan
    
  2. Create a phpstan.neon configuration file in your project root, and enable the rule for missing return types:
    parameters:
        level: 7 # Level 7+ enforces return types for all functions/methods
    rules:
        - PHPStan\Rules\Functions\MissingReturnTypeRule
    
  3. Run PHPStan:
    ./vendor/bin/phpstan analyse src/
    

This will scan your src/ directory and throw an error for every function (like your second add example) that doesn't declare a return type. You can integrate this into your CI/CD pipeline to block deployments if such issues are found.

Example with Psalm:

Psalm works similarly—set its strictness level to 1 or higher, and it will enforce return types by default. Install it via Composer:

composer require --dev vimeo/psalm

Run the initial setup, then configure your psalm.xml to enforce strict return type checks.

2. IDE Real-Time Checks

Most modern PHP IDEs (like PhpStorm) or code editors (VS Code with the PHP Intelephense plugin) let you enable real-time validation for missing return types. For example:

  • In PhpStorm: Go to Settings > Languages & Frameworks > PHP > Inspections > PHP > Type compatibility, and enable the "Missing return type declaration" inspection. Set it to "Error" severity to block you from committing code with this issue.
  • In VS Code with Intelephense: Enable the intelephense.diagnostics.missingReturnType setting in your settings.json.

3. Custom Code Sniffing (Advanced)

If you prefer using PHP_CodeSniffer, you can create a custom rule to check for missing return types. However, this requires more work compared to using a dedicated static analysis tool, so it's generally not recommended unless you have specific needs.

Why PHP Doesn't Have a Built-In Directive

PHP's core design prioritizes backward compatibility—millions of legacy projects rely on functions without return types. Adding a core directive to enforce return types would break this compatibility, so the PHP team instead encourages using static analysis tools to enforce strict coding standards for modern projects.

内容的提问来源于stack exchange,提问作者Xavi Montero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:11:02