Skip to content

Commit ac5c0c2

Browse files
Add load_mangled_object_vars(), the inverse of get_mangled_object_vars()
1 parent ae95644 commit ac5c0c2

4 files changed

+110
-2
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
--TEST--
2+
load_mangled_object_vars() function
3+
--FILE--
4+
<?php
5+
6+
#[AllowDynamicProperties]
7+
class A {
8+
public $pub = 1;
9+
protected $prot = 2;
10+
private $priv = 3;
11+
}
12+
class B extends A {
13+
private $priv = 4;
14+
}
15+
16+
$obj = new B;
17+
$obj->dyn = 5;
18+
$obj->{"6"} = 6;
19+
20+
$vars = get_mangled_object_vars($obj);
21+
22+
$obj2 = new B;
23+
$obj2->pub = 7;
24+
load_mangled_object_vars($obj2, $vars);
25+
26+
$vars2 = (array) $obj2;
27+
var_dump($vars2 === $vars);
28+
29+
#[AllowDynamicProperties]
30+
class AO extends ArrayObject {
31+
private $priv = 1;
32+
}
33+
34+
$ao = new AO(['x' => 'y']);
35+
$ao->dyn = 2;
36+
$vars = get_mangled_object_vars($ao);
37+
38+
$ao2 = new AO();
39+
load_mangled_object_vars($ao2, $vars);
40+
41+
$vars2 = get_mangled_object_vars($ao2);
42+
var_dump($vars2 === $vars);
43+
44+
class RO {
45+
public function __construct(public readonly int $ro = 123)
46+
{
47+
}
48+
}
49+
50+
$ro = new RO;
51+
load_mangled_object_vars($ro, []);
52+
var_dump($ro);
53+
54+
load_mangled_object_vars($ro, ['ro' => 'abc']);
55+
var_dump($ro);
56+
57+
#[AllowDynamicProperties]
58+
class DYN {
59+
public function __set($name, $value)
60+
{
61+
$this->data[$name] = $value;
62+
}
63+
}
64+
65+
$dyn = new DYN;
66+
load_mangled_object_vars($dyn, ['abc' => 123]);
67+
var_dump((array) $dyn);
68+
69+
?>
70+
--EXPECTF--
71+
bool(true)
72+
bool(true)
73+
object(RO)#%d (1) {
74+
["ro"]=>
75+
int(123)
76+
}
77+
object(RO)#%d (1) {
78+
["ro"]=>
79+
int(234)
80+
}
81+
array(1) {
82+
["abc"]=>
83+
int(123)
84+
}

Zend/zend_builtin_functions.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ ZEND_FUNCTION(get_object_vars)
801801
}
802802
/* }}} */
803803

804-
/* {{{ Returns an array of mangled object properties. Does not respect property visibility. */
804+
/* {{{ Returns an array of mangled object properties. Does not respect property visibility and does not call magic methods. */
805805
ZEND_FUNCTION(get_mangled_object_vars)
806806
{
807807
zend_object *obj;
@@ -825,6 +825,21 @@ ZEND_FUNCTION(get_mangled_object_vars)
825825
}
826826
/* }}} */
827827

828+
/* {{{ Loads an array of mangled object properties. Does not respect property visibility and does not call magic methods. */
829+
ZEND_FUNCTION(load_mangled_object_vars)
830+
{
831+
zend_object *obj;
832+
HashTable *vars;
833+
834+
ZEND_PARSE_PARAMETERS_START(2, 2)
835+
Z_PARAM_OBJ(obj)
836+
Z_PARAM_ARRAY_HT(vars)
837+
ZEND_PARSE_PARAMETERS_END();
838+
839+
object_properties_load(obj, vars);
840+
}
841+
/* }}} */
842+
828843
/* {{{ Returns an array of method names for class or class instance. */
829844
ZEND_FUNCTION(get_class_methods)
830845
{

Zend/zend_builtin_functions.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function get_object_vars(object $object): array {}
5959

6060
function get_mangled_object_vars(object $object): array {}
6161

62+
function load_mangled_object_vars(object $object, array $vars): void {}
63+
6264
/**
6365
* @return array<int, string>
6466
* @refcount 1

Zend/zend_builtin_functions_arginfo.h

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)