Where and why do we use __toString() in PHP?

后端 未结 10 1718
星月不相逢
星月不相逢 2021-01-01 10:34

I understand how it works but why would we practically use this?



        
10条回答
  •  青春惊慌失措
    2021-01-01 11:03

    __toString() is called when an object is passed to a function (esp echo() or print()) when its context is expected to be a string. Since an object is not a string, the __toString() handles the transformation of the object into some string representation.

提交回复
热议问题