|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2018-12-21 23:03 UTC] nikic@php.net
-PHP Version: 7.3.0
+PHP Version: 7.x
[2018-12-21 23:03 UTC] nikic@php.net
[2018-12-22 10:36 UTC] cmb@php.net
[2019-05-15 10:54 UTC] nikic@php.net
-Status: Open
+Status: Not a bug
[2019-05-15 10:54 UTC] nikic@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 03:00:01 2025 UTC |
Description: ------------ If an object has a __toString() method and is passed to a function as a parameter that is typehinted to string and passed by reference, then the object gets destroyed and replaced with the string. Without the typehint, the object passed by reference remains intact. Defining a typehint on a function's parameter should not impact the calling code beyond raising a TypeError exception. Test script: --------------- function one(&$str) { } function two(string &$str) { } class Foo { public function __toString() { return ''; } } $foo = new Foo(); echo gettype($foo); // object one($foo); echo gettype($foo); // object two($foo); echo gettype($foo); // string Expected result: ---------------- The passed object should not be destroyed. The typehint should be used only to verify that the parameter meets a requirement, not to actively change its type. In addition, if overwriting the object is determined to be the correct behavior, then I feel it should at least raise an E_NOTICE when changing a variable's type.