Skip to content

Commit eecd896

Browse files
committed
Add get_mangled_object_vars() function
1 parent 3ff489d commit eecd896

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

UPGRADING

+6
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ PHP 7.4 UPGRADE NOTES
337337
6. New Functions
338338
========================================
339339

340+
- Core:
341+
. Added get_mangled_object_vars($object) function, which returns the mangled
342+
object properties. It returns the same result as (array) $object, with the
343+
exception that it ignores overloaded array casts, such as used by
344+
ArrayObject.
345+
340346
- OpenSSL:
341347
. Added openssl_x509_verify(mixed cert, mixed key) function that verifies the
342348
signature of the certificate using a public key. A wrapper around the
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
get_mangled_object_vars() function
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
public $pub = 1;
8+
protected $prot = 2;
9+
private $priv = 3;
10+
}
11+
class B extends A {
12+
private $priv = 4;
13+
}
14+
15+
$obj = new B;
16+
$obj->dyn = 5;
17+
$obj->{"6"} = 6;
18+
19+
var_export(get_mangled_object_vars($obj));
20+
echo "\n";
21+
22+
class AO extends ArrayObject {
23+
private $priv = 1;
24+
}
25+
26+
$ao = new AO(['x' => 'y']);
27+
$ao->dyn = 2;
28+
var_export(get_mangled_object_vars($ao));
29+
echo "\n";
30+
var_export((array) $ao);
31+
echo "\n";
32+
33+
?>
34+
--EXPECT--
35+
array (
36+
'' . "\0" . 'B' . "\0" . 'priv' => 4,
37+
'pub' => 1,
38+
'' . "\0" . '*' . "\0" . 'prot' => 2,
39+
'' . "\0" . 'A' . "\0" . 'priv' => 3,
40+
'dyn' => 5,
41+
6 => 6,
42+
)
43+
array (
44+
'' . "\0" . 'AO' . "\0" . 'priv' => 1,
45+
'dyn' => 2,
46+
)
47+
array (
48+
'x' => 'y',
49+
)

Zend/zend_builtin_functions.c

+31
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static ZEND_FUNCTION(is_subclass_of);
5656
static ZEND_FUNCTION(is_a);
5757
static ZEND_FUNCTION(get_class_vars);
5858
static ZEND_FUNCTION(get_object_vars);
59+
static ZEND_FUNCTION(get_mangled_object_vars);
5960
static ZEND_FUNCTION(get_class_methods);
6061
static ZEND_FUNCTION(trigger_error);
6162
static ZEND_FUNCTION(set_error_handler);
@@ -145,6 +146,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_get_object_vars, 0, 0, 1)
145146
ZEND_ARG_INFO(0, obj)
146147
ZEND_END_ARG_INFO()
147148

149+
ZEND_BEGIN_ARG_INFO_EX(arginfo_get_mangled_object_vars, 0, 0, 1)
150+
ZEND_ARG_INFO(0, obj)
151+
ZEND_END_ARG_INFO()
152+
148153
ZEND_BEGIN_ARG_INFO_EX(arginfo_get_class_methods, 0, 0, 1)
149154
ZEND_ARG_INFO(0, class)
150155
ZEND_END_ARG_INFO()
@@ -264,6 +269,7 @@ static const zend_function_entry builtin_functions[] = { /* {{{ */
264269
ZEND_FE(is_a, arginfo_is_subclass_of)
265270
ZEND_FE(get_class_vars, arginfo_get_class_vars)
266271
ZEND_FE(get_object_vars, arginfo_get_object_vars)
272+
ZEND_FE(get_mangled_object_vars, arginfo_get_mangled_object_vars)
267273
ZEND_FE(get_class_methods, arginfo_get_class_methods)
268274
ZEND_FE(trigger_error, arginfo_trigger_error)
269275
ZEND_FALIAS(user_error, trigger_error, arginfo_trigger_error)
@@ -1238,6 +1244,31 @@ ZEND_FUNCTION(get_object_vars)
12381244
}
12391245
/* }}} */
12401246

1247+
/* {{{ proto array get_mangled_object_vars(object obj)
1248+
Returns an array of mangled object properties. Does not respect property visibility. */
1249+
ZEND_FUNCTION(get_mangled_object_vars)
1250+
{
1251+
zval *obj;
1252+
HashTable *properties;
1253+
1254+
ZEND_PARSE_PARAMETERS_START(1, 1)
1255+
Z_PARAM_OBJECT(obj)
1256+
ZEND_PARSE_PARAMETERS_END();
1257+
1258+
properties = Z_OBJ_HT_P(obj)->get_properties(obj);
1259+
if (!properties) {
1260+
ZVAL_EMPTY_ARRAY(return_value);
1261+
return;
1262+
}
1263+
1264+
properties = zend_proptable_to_symtable(properties,
1265+
(Z_OBJCE_P(obj)->default_properties_count ||
1266+
Z_OBJ_P(obj)->handlers != &std_object_handlers ||
1267+
GC_IS_RECURSIVE(properties)));
1268+
RETURN_ARR(properties);
1269+
}
1270+
/* }}} */
1271+
12411272
static int same_name(zend_string *key, zend_string *name) /* {{{ */
12421273
{
12431274
zend_string *lcname;

ext/opcache/Optimizer/zend_func_info.c

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ static const func_info_t func_infos[] = {
239239
F0("is_a", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_TRUE), // TODO: inline
240240
F1("get_class_vars", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_STRING | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF),
241241
FN("get_object_vars", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF),
242+
FN("get_mangled_object_vars", MAY_BE_NULL | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF),
242243
F1("get_class_methods", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_STRING),
243244
F0("method_exists", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_TRUE),
244245
F0("property_exists", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_TRUE),

0 commit comments

Comments
 (0)