1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 08:12:21 +01:00
Files
archived-php-src/ext/spl/tests/fixedarray_023.phpt
Marco Pivetta 25cb9cdb79 Fix GH-8232 - always reference classes in var_export() via their FQCN
Closes GH-8233

This fix corrects a behavior of `var_export()` that was mostly "hidden" until PHP 8.1 introduced:

* properties with object initializers
* constants containing object references
* default values of class properties containing `enum`s

Since `var_export(..., true)` is mostly used in conjunction with code generation,
and we cannot make assumptions about the generated code being placed in the root
namespace, we must always provide the FQCN of a class in exported code.

For example:

```php
<?php

namespace MyNamespace { class Foo {} }

namespace { echo "<?php\n\nnamespace Example;\n\n" . var_export(new \MyNamespace\Foo(), true) . ';'; }
```

produces:

```php
<?php

namespace Example;

MyNamespace\Foo::__set_state(array(
));
```

This code snippet is invalid, because `Example\MyNamespace\Foo::__set_state()` (which
does not exist) is called.

With this patch applied, the code looks like following (valid):

```php
<?php

namespace Example;

\MyNamespace\Foo::__set_state(array(
));
```

Ref: https://github.com/php/php-src/issues/8232
Ref: https://github.com/Ocramius/ProxyManager/issues/754
Ref: https://externals.io/message/117466
2022-04-23 11:06:21 +02:00

36 lines
750 B
PHP

--TEST--
SPL: FixedArray: Infinite loop in var_export bugfix
--FILE--
<?php
call_user_func(function () {
$x = new SplFixedArray(4);
$x[0] = NAN; // Test NAN just in case this check is incorrectly refactored to use zend_is_identical
$x[1] = 0.0;
$x[2] = $x;
$x[3] = $x;
var_export($x);
echo "\n";
$x[1] = -0.0;
debug_zval_dump($x);
});
?>
--EXPECTF--
Warning: var_export does not handle circular references in %s on line 8
Warning: var_export does not handle circular references in %s on line 8
\SplFixedArray::__set_state(array(
0 => NAN,
1 => 0.0,
2 => NULL,
3 => NULL,
))
object(SplFixedArray)#2 (4) refcount(6){
[0]=>
float(NAN)
[1]=>
float(-0)
[2]=>
*RECURSION*
[3]=>
*RECURSION*
}