note 75682 added to function.strval
| From: | NyctoFixer at gmail dot com | Date: | Mon, 11 Jun 2007 19:19:48 +0000 |
| Subject: | note 75682 added to function.strval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127401@lists.php.net to get a copy of this message | ||
As of PHP 5.1.4 (I have not tested it in later versions), the strval function does not attempt to
invoke the __toString method when it encounters an object. This simple wrapper function will handle
this circumstance for you:
<?
/**
* Returns the string value of a variable
*
* This differs from strval in that it invokes __toString if an object is given
* and the object has that method
*/
function stringVal ($value)
{
// We use get_class_methods instead of method_exists to ensure that __toString is a public
method
if (is_object($value) && in_array("__toString", get_class_methods($value)))
return strval($value->__toString());
else
return strval($value);
}
?>
----
Server IP: 69.39.81.135
Probable Submitter: 66.191.5.181
----
Manual Page -- http://www.php.net/manual/en/function.strval.php
Edit -- https://master.php.net/note/edit/75682
Del: integrated -- https://master.php.net/note/delete/75682/integrated
Del: useless -- https://master.php.net/note/delete/75682/useless
Del: bad code -- https://master.php.net/note/delete/75682/bad+code
Del: spam -- https://master.php.net/note/delete/75682/spam
Del: non-english -- https://master.php.net/note/delete/75682/non-english
Del: in docs -- https://master.php.net/note/delete/75682/in+docs
Del: other reasons-- https://master.php.net/note/delete/75682
Reject -- https://master.php.net/note/reject/75682
Search -- https://master.php.net/manage/user-notes.php