Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bug #62328 (implementing __toString and a cast to string fails) #157

Merged
merged 2 commits into from

4 participants

@lt
lt commented

https://bugs.php.net/bug.php?id=62328

When extending a built-in class that has a cast_object function, and specifying a __toString method, the __toString method is ignored.

I have added a check to see if the object implements a __toString magic method, and call it when it does, leaving the check for cast_object as the next thing to check.

No new tests are failing with this change.

@lt lt zend_make_printable_zval choses cast_object over __toString
https://bugs.php.net/bug.php?id=62328

Added a check to see if the object implements a __toString magic
method. This should be called instead of the cast_object method of
built-in classes when defined.
a5dfd41
@reeze

Maybe one more test case for this :)

@lt
lt commented

@reeze The specific test case used SimpleXmlElement. Is it OK to create a test using this and skip it if the module is disabled? Or should I look for a better example class that handles it's own casting?

Edit

SplFileInfo looks like a good candidate.

@reeze

maybe you could find a class from spl classes :)

@php-pulls php-pulls merged commit 222ab9d into from
@laruence
Owner

sorry, I merged this for reviewing, but push to master by accident, anyway, I got a different fix here: 7b307fb thanks .

@lt
lt commented

Hi Laruence.

I didn't fully understand the impact of removing the lower block of code. I did the same as you originally, but in the end I left it in simply because I didn't understand what other cases it might cater for.

Please consider using SplFileInfo instead of SimpleXmlElement in the test for this. XML libraries may be disabled, but SPL will not.

@laruence
Owner

leight, hmm, okey, I think this test can be merged into ext/spl folder too , will do it . thanks

@laruence
Owner

test script merged e51acee thanks

@lt lt deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 10, 2012
  1. @lt

    zend_make_printable_zval choses cast_object over __toString

    lt authored
    https://bugs.php.net/bug.php?id=62328
    
    Added a check to see if the object implements a __toString magic
    method. This should be called instead of the cast_object method of
    built-in classes when defined.
  2. @lt

    Test for bug 62328

    lt authored
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 0 deletions.
  1. +22 −0 Zend/tests/bug62328.phpt
  2. +6 −0 Zend/zend.c
View
22 Zend/tests/bug62328.phpt
@@ -0,0 +1,22 @@
+--TEST--
+Bug #62328 (cast_object takes precedence over __toString)
+--FILE--
+<?php
+
+class SplFileInfo62328 extends SplFileInfo
+{
+ public function __toString()
+ {
+ return '__toString';
+ }
+}
+
+$fi = new SplFileInfo62328(__FILE__);
+
+echo (string)$fi . PHP_EOL;
+echo (string)$fi->__toString() . PHP_EOL;
+
+?>
+--EXPECT--
+__toString
+__toString
View
6 Zend/zend.c
@@ -258,6 +258,12 @@ ZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_cop
{
TSRMLS_FETCH();
+ if (Z_OBJCE_P(expr)->__tostring) {
+ if (zend_std_cast_object_tostring(expr, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
+ break;
+ }
+ }
+
if (Z_OBJ_HANDLER_P(expr, cast_object)) {
zval *val;
Something went wrong with that request. Please try again.