From:             oliver dot graetz at gmx dot de
Operating system: any
PHP version:      5.2.1
PHP Bug Type:     Feature/Change Request
Bug description:  change string conversion behaviour for objects not 
implementing __toString()

Description:
------------
Yes, I read the upgrade guide and the other bug reports regarding this
topic so this is not a bug report but a plea for reconsideration.

I really like that finally __toString() works in every situation but the
inability to output object instances without __toString() defined is just
too annoying. PHP preaches the KISS principle and on this issue the
language is breaking its own rules.

First of all, there are engine internal classes where the programmer is
unable to provide a __toString method. Subclassing all of these classes
upon usage just "to be on the safe side" is nonsense. If object output
can't be changed to provide a fallback if __toString() is missing then at
least all engine internal classes should implement their own default
output.

Secondly, for safety many programmers might be tempted to make all classes
extend a common superclass just "to be on the safe side". This is braindead
for the sake of any OOP concept but I already see some guys on the horizon
ready to do it.

And at last: There are so many convenient functions that just break if
their input contains "problem objects". It just makes no sense that PHP
forces me to implement an "object safe" version of implode()! I just had
to do that and the loss of performance makes me shudder. Rasmus once said
that PHP should only be a frontend for "PHP templates" that make use of as
much precompiled code as possible. So why are these "templates" forced to
implement ever more stuff in the userland?

Suggestions:

- at least implement default output for all engine internal classes

- change __toString() to have a fallback, even "[__toString() missing]"
improves on the current situation

-- if this isn't POSSIBLE: PLEASE clearly state why at prominent places in
the documentation.

-- if this isn't WANTED: make it configurable or better, add a magic
function, for example __tostring_fallback(), which should return a string.
If it doesn't exist or doesn't return a string: go ahead raising the
recoverable error! Abusing an error handler to do this is NOT a solution.


Reproduce code:
---------------
<?php
echo new DateTime();
/*
 * How about this yielding the ISO formatted date or a timestamp?
 * Possibly even as debug output like
 * 
 * [DateTime object: 2004-02-12T15:19:21+00:00]
 * 
 * Definitely better than raising an error!
 */


Expected result:
----------------
some output and the script continuing

Actual result:
--------------
PHP Catchable fatal error:  Object of class DateTime could not be
converted to string in C:\test.php on line 2

Catchable fatal error: Object of class DateTime could not be converted to
string in C:\test.php on line 2

-- 
Edit bug report at http://bugs.php.net/?id=40799&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=40799&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=40799&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=40799&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=40799&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=40799&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=40799&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=40799&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=40799&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=40799&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=40799&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=40799&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=40799&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=40799&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=40799&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=40799&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=40799&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=40799&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=40799&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=40799&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=40799&r=mysqlcfg

Reply via email to