how to create a php class which can be casted to boolean (be truthy or falsy)

前端 未结 6 1000
闹比i
闹比i 2021-01-17 16:11

I am creating a collection class and would like it to be drop-in-replacement for arrays, which I use currently.

How to create a class which could be casted

6条回答
  •  無奈伤痛
    2021-01-17 16:17

    You can take a look at the PHP operator extension which you can use to overload many operators including == and ===. With this extension, you should be theoretically able to write a class comparable to boolean values like this:

    if($object == true)
    

提交回复
热议问题