Last active
December 26, 2015 03:09
-
-
Save lsjroberts/7084126 to your computer and use it in GitHub Desktop.
PHP __toString() bubble test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Foo { | |
public $bar; | |
public function __construct() | |
{ | |
$this->bar = new Bar; | |
} | |
public function __toString() | |
{ | |
return $this->bar; | |
} | |
} | |
class Bar { | |
public $value; | |
public function __construct() | |
{ | |
$this->value = "Hello"; | |
} | |
public function __toString() | |
{ | |
return $this->value; | |
} | |
} | |
var_dump((string) new Foo); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expected output is the value of
$this->bar->value
, but actually throwsCatchable fatal error: Method Foo::__toString() must return a string value
.Can be fixed by changing the
Foo::__toString()
to return(string) $this->bar
explicitly.