Bug #80039 [Opn]: Illegal string offset and Cannot create references to/from string offsets
Edit report at https://bugs.php.net/bug.php?id=80039&edit=1
ID: 80039
Updated by: nikic@php.net
Reported by: miloslav dot hula at gmail dot com
Summary: Illegal string offset and Cannot create references
to/from string offsets
Status: Open
Type: Bug
Package: *General Issues
PHP Version: 7.4.9
Block user comment: N
Private report: N
New Comment:
Hm, I can't reproduce this on current 7.4/master HEAD, just get empty output. Possibly this has
been fixed recently?
Previous Comments:
------------------------------------------------------------------------
[2020-08-31 09:03:06] miloslav dot hula at gmail dot com
Description:
------------
This is probably related to https://bugs.php.net/bug.php?id=80037
The test scripts emits warning and fatal error when creating a reference to static variable.
The test script is reduced to show the error. In production code, there is a relation between
referenced value in fromArray() and $prop = &$cache ...
For example, when I call:
A::fromArray(['a' => 'foo']);
the error is emmited, the exception handler executed and
$prop = &$cache[$class][$name];
normally pass. But, the $prop contains 'foo' string.
Test script:
---------------
<?php
final class A
{
public string $a;
public static function fromArray(array $props): self
{
$me = new static;
foreach ($props as $k => &$v) {
$me->{$k} = &$v;
}
return $me;
}
public function __get($name)
{
throw new \LogicException("Property '$name' is not defined.");
}
}
class ObjectHelpers
{
public static function hasProperty(string $class, string $name)
{
static $cache = [];
$prop = &$cache[$class][$name]; # <-- emits error
}
}
set_exception_handler(function ($e) {
ObjectHelpers::hasProperty(A::class, 'a');
});
A::fromArray(['a' => 'foo']);
Expected result:
----------------
(no error)
Actual result:
--------------
Warning: Illegal string offset 'a' in bug.php on line 27
Fatal error: Uncaught Error: Cannot create references to/from string offsets in bug.php:27
Stack trace:
#0 bug.php(32): ObjectHelpers::hasProperty('A', 'a')
#1 [internal function]: {closure}(Object(Error))
#2 {main}
thrown in bug.php on line 27
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80039&edit=1
Thread (9 messages)