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

前端 未结 6 956
闹比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:25

    If you have a code size of "many megabytes" the issue is probably not that your naming scheme is too verbose. I would instead look for duplication and try to abstract code.

    OTOH, why is the size of the code a big issue? Try to minimize the code you are including each time php runs. Stray code that isn't used makes no difference. If you have to include a lot of code, consider using caching software, such as MMCache.

    And to answer your original question, AFAIK there is no way to add type coercion to PHP classes. an instance will always evaluate to true.

提交回复
热议问题