Is it possible to write strictly typed PHP code?

后端 未结 9 850
陌清茗
陌清茗 2020-12-14 15:34

For example, is it possible to write code like this:

int $x = 6;
str $y = \"hello world\";
bool $z = false;
MyObject $foo = new MyObject();

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 15:59

    In PHP 7 are implemented "Scalar Type Declarations", e.g.:

    public function getBalance(): int {
        return 555;
    }
    

    You need to declare, that you will use strict types:

    
    

    More information: https://wiki.php.net/rfc/scalar_type_hints_v5

提交回复
热议问题