Skip to content

Commit 7b307fb

Browse files
committed
Fixed bug #62328 (implementing __toString and a cast to string fails)
__toString should has a high priority
1 parent d4f9bbf commit 7b307fb

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ PHP NEWS
33
?? ??? 2012, PHP 5.4.7
44

55
- Core:
6+
. Fixed bug #62328 (implementing __toString and a cast to string fails)
7+
(Laruence)
68
. Fixed bug #62725 (Calling exit() in a shutdown function does not return
79
the exit value). (Laruence)
810
. Fixed bug #51363 (Fatal error raised by var_export() not caught by error

Zend/zend.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ ZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_cop
258258
{
259259
TSRMLS_FETCH();
260260

261+
if (zend_std_cast_object_tostring(expr, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
262+
break;
263+
}
261264
if (Z_OBJ_HANDLER_P(expr, cast_object)) {
262265
zval *val;
263266

@@ -270,12 +273,6 @@ ZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_cop
270273
}
271274
zval_ptr_dtor(&val);
272275
}
273-
/* Standard PHP objects */
274-
if (Z_OBJ_HT_P(expr) == &std_object_handlers || !Z_OBJ_HANDLER_P(expr, cast_object)) {
275-
if (zend_std_cast_object_tostring(expr, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
276-
break;
277-
}
278-
}
279276
if (!Z_OBJ_HANDLER_P(expr, cast_object) && Z_OBJ_HANDLER_P(expr, get)) {
280277
zval *z = Z_OBJ_HANDLER_P(expr, get)(expr TSRMLS_CC);
281278

ext/xml/tests/bug62328.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #62328 (implementing __toString and a cast to string fails)
3+
--SKIPIF--
4+
<?php
5+
require_once("skipif.inc");
6+
?>
7+
--FILE--
8+
<?php
9+
class UberSimpleXML extends SimpleXMLElement {
10+
public function __toString() {
11+
return 'stringification';
12+
}
13+
}
14+
15+
$xml = new UberSimpleXML('<xml/>');
16+
17+
var_dump((string) $xml);
18+
var_dump($xml->__toString());
19+
--EXPECT--
20+
string(15) "stringification"
21+
string(15) "stringification"

0 commit comments

Comments
 (0)