Declaring Variable Types in PHP?

后端 未结 5 606
灰色年华
灰色年华 2021-02-03 17:19

I was trying to get my Netbeans to autocomplete with PHP, and I learned that this code is valid in PHP:

function blah(Bur $bur) {}

A couple of

5条回答
  •  孤独总比滥情好
    2021-02-03 18:15

    Does this actually impose any limits on what type of variable I can pass to the blah method?

    This is called type hinting. According to the PHP documentation that I just linked to, yes, it does impose limits on the argument type: "Failing to satisfy the type hint results in a catchable fatal error."

    How can I declare the type of a variable in PHP if I'm not in a function?

    Read type juggling. You can't explicitly define a variable's type in PHP, its type is decided by the context it is used in.

提交回复
热议问题