Bug #77336 [NEW]: objects with __toString() get destroyed when typehinted and passed by reference

From: Date: Fri, 21 Dec 2018 22:47:14 +0000
Subject: Bug #77336 [NEW]: objects with __toString() get destroyed when typehinted and passed by reference
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-218561@lists.php.net to get a copy of this message
From:             alex dot howansky at gmail dot com
Operating system: Ubuntu 16.04
PHP version:      7.3.0
Package:          Scripting Engine problem
Bug Type:         Bug
Bug description:objects with __toString() get destroyed when typehinted and passed by reference

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.


-- 
Edit bug report at https://bugs.php.net/bug.php?id=77336&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=77336&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=77336&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=77336&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=77336&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=77336&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=77336&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=77336&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=77336&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=77336&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=77336&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=77336&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=77336&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=77336&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77336&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=77336&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=77336&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=77336&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77336&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=77336&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=77336&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#218561) next »