Is it possible to write strictly typed PHP code?

后端 未结 9 876
陌清茗
陌清茗 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 16:10

    Edit: This answer applies to versions of PHP 5.6 and earlier. As noted in recent answers, PHP version 7.0 and later does have some support for this


    Original answer:

    No. There is only support for type hinting since php5, but "Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported."

    In PHP 7 are implemented "Scalar Type Declarations" see the answer below.

    That is as far as php currently goes, and as far as it should go if you ask me.

提交回复
热议问题