On Wed, Feb 25, 2015 at 5:23 PM, Niktia Nefedov <inefe...@gmail.com> wrote:
> On Wed, 25 Feb 2015 17:54:21 +0400, Dmitry Stogov <dmi...@zend.com> wrote: > >> The object on the call-site should remain to be an object (if it's not >> passed by reference), however the called function will receive a string. >> It works in PHP-5 and PHP-7. Nothing should be changed. >> $ sapi/cli/php -r 'class X {function __toString(){return "abc";}} $x=new >> X; var_dump(strlen($x)); var_dump($x);' >> int(3) >> object(X)#1 (0) { >> } >> However, declare(strict_types=1) will break this. see >> https://github.com/ircmaxell/php-src/compare/scalar_type_hints_v5#diff- >> ef5bf53d1412b50f85d125ca4fe84741R1182 >> Thanks. Dmitry. >> > > Dmitry, I was talking about passing an object to a function, expecting a > reference of a string: > > 'class X { function __toString() { return spl_object_hash($this); } } $x = > new X; function foo(string &$x){} foo($x); var_dump($x);' > > Now what would this produce in Coercive STH is still an open question I > guess as there is no implementation yet. > This will change $x because it's passed by reference. This is the common "problem" for all current proposals, because for parameters passed by reference we check only "input" types, and don't guarantee anything about output. <?php declare(strict_types=1); function foo(string &$x) { $x = 321; } $x = "123"; foo($x); var_dump($x); // -> int(321) ?> Thanks. Dmitry.