Skip to content

Commit

Permalink
Add load_mangled_object_vars(), the inverse of get_mangled_object_vars()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Aug 23, 2022
1 parent ae95644 commit ac5c0c2
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
84 changes: 84 additions & 0 deletions Zend/tests/load_mangled_object_vars.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
--TEST--
load_mangled_object_vars() function
--FILE--
<?php

#[AllowDynamicProperties]
class A {
public $pub = 1;
protected $prot = 2;
private $priv = 3;
}
class B extends A {
private $priv = 4;
}

$obj = new B;
$obj->dyn = 5;
$obj->{"6"} = 6;

$vars = get_mangled_object_vars($obj);

$obj2 = new B;
$obj2->pub = 7;
load_mangled_object_vars($obj2, $vars);

$vars2 = (array) $obj2;
var_dump($vars2 === $vars);

#[AllowDynamicProperties]
class AO extends ArrayObject {
private $priv = 1;
}

$ao = new AO(['x' => 'y']);
$ao->dyn = 2;
$vars = get_mangled_object_vars($ao);

$ao2 = new AO();
load_mangled_object_vars($ao2, $vars);

$vars2 = get_mangled_object_vars($ao2);
var_dump($vars2 === $vars);

class RO {
public function __construct(public readonly int $ro = 123)
{
}
}

$ro = new RO;
load_mangled_object_vars($ro, []);
var_dump($ro);

load_mangled_object_vars($ro, ['ro' => 'abc']);
var_dump($ro);

#[AllowDynamicProperties]
class DYN {
public function __set($name, $value)
{
$this->data[$name] = $value;
}
}

$dyn = new DYN;
load_mangled_object_vars($dyn, ['abc' => 123]);
var_dump((array) $dyn);

?>
--EXPECTF--
bool(true)
bool(true)
object(RO)#%d (1) {
["ro"]=>
int(123)
}
object(RO)#%d (1) {
["ro"]=>
int(234)
}
array(1) {
["abc"]=>
int(123)
}
17 changes: 16 additions & 1 deletion Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ ZEND_FUNCTION(get_object_vars)
}
/* }}} */

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

/* {{{ Loads an array of mangled object properties. Does not respect property visibility and does not call magic methods. */
ZEND_FUNCTION(load_mangled_object_vars)
{
zend_object *obj;
HashTable *vars;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_OBJ(obj)
Z_PARAM_ARRAY_HT(vars)
ZEND_PARSE_PARAMETERS_END();

object_properties_load(obj, vars);
}
/* }}} */

/* {{{ Returns an array of method names for class or class instance. */
ZEND_FUNCTION(get_class_methods)
{
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function get_object_vars(object $object): array {}

function get_mangled_object_vars(object $object): array {}

function load_mangled_object_vars(object $object, array $vars): void {}

/**
* @return array<int, string>
* @refcount 1
Expand Down
9 changes: 8 additions & 1 deletion Zend/zend_builtin_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac5c0c2

Please sign in to comment.