Implicit Type Conversion for PHP Classes?

前端 未结 7 1136
轻奢々
轻奢々 2021-01-25 06:16

Is there a way to tell the php complier that I want a specific implicit conversion from one type to another?

A simple example:

class Integer
{
  public $         


        
7条回答
  •  攒了一身酷
    2021-01-25 06:48

    Are you asking how to type cast? You can do:

    $ php -r 'var_dump("333");'
    string(3) "333"
    $ php -r 'var_dump((int)"333");'
    int(333)
    

    Otherwise PHP is weakly typed, so in general you don't need to do it. It's implied by the language. Eg. if a function takes a numeric argument, then that number can be string, integer or float all the same; The value is converted if needed. That's why you can for example echo 33 without an error.

提交回复
热议问题