[ create a new paste ] login | about

Link: http://codepad.org/qkfe4XlH    [ raw code | output | fork ]

php.programmer - PHP, pasted on Apr 15:
<?php
/* sample code to show use of magic methods <b>__toString()</b>.
 * http://royads.blogspot.com
*/
class stringTest
{
  public function __toString()
   { 
     return 'It can\'t be echo like this';
   }
} 

class stringTest2
{
} 

$obj = new stringTest();
echo $obj."\n\n\n";

$obj = new stringTest2();
echo $obj;
?>


Output:
1
2
3
4
5
It can't be echo like this



Catchable fatal error: Object of class stringTest2 could not be converted to string on line 21


Create a new paste based on this one


Comments: