Implicit Type Conversion for PHP Classes?

前端 未结 7 1125
轻奢々
轻奢々 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 07:06

    PHP is not strongly typed.

    You want something like, whenever you pass an hbool instance into a function that expects a bool it would convert automatically using your implicit conversor.

    What happens is that no function "expects" a bool, it's all dynamically typed. You can force an explicit conversion by calling (int)$something but if that's the case you can then pass $something->to_otherthing() instead for non-automatic conversions.

    PS: I'm sure im not being clear here. I will try to edit this answer after I eat something :)

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