1
0
mirror of https://github.com/php/php-src.git synced 2026-04-11 01:53:36 +02:00
Files
archived-php-src/ext/spl/tests/SplFixedArray_get_properties_for.phpt
Tyson Andre c4ecd82f93 Make inspecting SplFixedArray instances more memory efficient/consistent, change print_r null props handling (#9757)
* Make handling of SplFixedArray properties more consistent

Create a brand new reference counted array every time in SplFixedArray
to be freed by the callers (or return null).
Switch from overriding `get_properties` to overriding `get_properties_for` handler

* Print objects with null hash table like others in print_r

Noticed when working on subsequent commits for SplFixedArray.
Make whether zend_get_properties_for returns null or an empty array
invisible to the end user - it would be always be a non-null array for
user-defined classes.
Always print newlines with `\n\s*(\n\s*)` after objects

Noticed when working on SplFixedArray changes, e.g. in
ext/spl/tests/SplFixedArray__construct_param_null.phpt
2022-10-24 08:33:25 -04:00

87 lines
2.0 KiB
PHP

--TEST--
SplFixedArray - get_properties_for handlers
--FILE--
<?php
#[AllowDynamicProperties]
class MySplFixedArray extends SplFixedArray {
public $x;
public int $y;
}
class X {}
class Y {}
$array = new MySplFixedArray(2);
var_dump(get_mangled_object_vars($array));
$array[0] = new stdClass();
$array[1] = new Y();
$array->x = new SplFixedArray();
$array->{0} = new X();
var_dump($array);
// As of php 8.3, get_mangled_object_vars only contains object properties (dynamic properties and declared subclass properties)
// (Array elements in the SplFixedArray are deliberately excluded)
// Before php 8.3, this would have array elements get removed in some cases but not others.
var_dump(get_mangled_object_vars($array));
echo "cast to array\n";
var_dump((array)$array); // Adds the values from the underlying array, then the declared/dynamic object properties
echo json_encode($array), "\n"; // From JsonSerializable::serialize()
$ser = serialize($array);
echo "$ser\n";
// NOTE: The unserialize behavior for the property that is the string '0' is just because unserialize()
// is coercing '0' to a string before calling SplFixedArray::__unserialize.
//
// Typical code would not use 0 as a property name, this test is just testing edge cases have proper reference counting and so on.
var_dump(unserialize($ser));
?>
--EXPECT--
array(1) {
["x"]=>
NULL
}
object(MySplFixedArray)#1 (4) {
[0]=>
object(stdClass)#2 (0) {
}
[1]=>
object(Y)#3 (0) {
}
["x"]=>
object(SplFixedArray)#4 (0) {
}
["0"]=>
object(X)#5 (0) {
}
}
array(2) {
["x"]=>
object(SplFixedArray)#4 (0) {
}
[0]=>
object(X)#5 (0) {
}
}
cast to array
array(3) {
[0]=>
object(X)#5 (0) {
}
[1]=>
object(Y)#3 (0) {
}
["x"]=>
object(SplFixedArray)#4 (0) {
}
}
[{},{}]
O:15:"MySplFixedArray":5:{i:0;O:8:"stdClass":0:{}i:1;O:1:"Y":0:{}s:1:"x";i:0;s:1:"y";i:0;s:1:"0";O:1:"X":0:{}}
object(MySplFixedArray)#6 (4) {
[0]=>
object(X)#9 (0) {
}
[1]=>
object(Y)#8 (0) {
}
["x"]=>
int(0)
["y"]=>
int(0)
}