PHP - Force function parameter to integer

前端 未结 2 1795
情话喂你
情话喂你 2020-12-12 05:04

In PHP you can do the following thing:

class Something {// bla bla}

function functionName(Something $object) {
    // Do stuff here  ^^^^^^^^^
}


        
相关标签:
2条回答
  • 2020-12-12 05:14

    You can't typehint scalar types in PHP. The only chance is to use a wrapper type. The SPL library ships with SplInt for that purpose.

    Update: Looks like PHP 7 offers that feature. Sigh, times changed :)

    0 讨论(0)
  • 2020-12-12 05:31

    You can't do this for scalar types see the manual:

    And a quote from there:

    Type hints cannot be used with scalar types such as int or string. Resources and Traits are not allowed either.

    But you will be able to to this with PHP 7: https://wiki.php.net/rfc/scalar_type_hints_v5

    0 讨论(0)
提交回复
热议问题