Skip to content

Instantly share code, notes, and snippets.

@lsjroberts
Last active December 26, 2015 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsjroberts/7084126 to your computer and use it in GitHub Desktop.
Save lsjroberts/7084126 to your computer and use it in GitHub Desktop.
PHP __toString() bubble test
<?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;
@lsjroberts
Copy link
Author

Expected output is the value of $this->bar->value, but actually throws Catchable fatal error: Method Foo::__toString() must return a string value.

Can be fixed by changing the Foo::__toString() to return (string) $this->bar explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment