0

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?

Dan
  • 10,614
  • 5
  • 24
  • 35
  • Read the [documentation about magic methods](http://php.net/manual/language.oop5.magic.php), please. – ComFreek Mar 31 '14 at 19:46

2 Answers2

4

__toString() is what you are looking for.

http://www.php.net/manual/en/language.oop5.magic.php#object.tostring

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
0

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.)

Schleis
  • 41,516
  • 7
  • 68
  • 87