I am trying to apply a python concept in PHP. In python we can override the __str__
method of a class to change the string representation of an object.
Is there a similar concept in PHP and what does the syntax look like?
I am trying to apply a python concept in PHP. In python we can override the __str__
method of a class to change the string representation of an object.
Is there a similar concept in PHP and what does the syntax look like?
__toString()
is what you are looking for.
http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
There are a limited number of methods that you can define for a class via the magic methods.
http://php.net/manual/en/language.oop5.magic.php
PHP allows you to define functions for these methods only. You are not able to override the number of methods like in Python (mathematical operations, comparisons, etc.)