Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load_mangled_object_vars(), the inverse of get_mangled_object_vars() #9408

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just here to prove that the implementation is incomplete for now, see PR description.

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.