8000 Don't overload ArrayObject get_properties by nikic · Pull Request #3574 · php/php-src · GitHub
[go: up one dir, main page]

Skip to content

Don't overload ArrayObject get_properties #3574

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Don't overload get_properties in ArrayObject
Instead explicitly overload the (array) cast.
  • Loading branch information
nikic committed Oct 4, 2018
commit 53ba6bc5ad04dfe9772fb89aec2fb03e3c4dc24f
14 changes: 14 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ PHP 7.4 UPGRADE NOTES
supported and resulted in corrupted reflection objects. It has been
explicitly prohibited now.

- SPL:
. Calling get_object_vars() on an ArrayObject instance will now always return
the properties of the ArrayObject itself (or a subclass). Previously it
returned the values of the wrapped array/object unless the STD_PROP_LIST
flag was specified. Other affected operations are:
* ReflectionObject::getProperties()
* array_key_exists(). Use isset() or offsetExists() instead.
* reset(), current(), etc. Use Iterator methods instead.
* Potentially others working on object properties as a list.

(array) casts are *not* affected. They will continue to return either the
wrapped array, or the ArrayObject properties, depending on whether the
STD_PROP_LIST flag is used.

========================================
2. New Features
========================================
Expand Down
6 changes: 0 additions & 6 deletions ext/reflection/tests/bug61388.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ print_r($reflObj->getProperties(ReflectionProperty::IS_PUBLIC));
--EXPECT--
Array
(
[0] => ReflectionProperty Object
(
[name] => test
[class] => ArrayObject
)

)
Array
(
Expand Down
28 changes: 13 additions & 15 deletions ext/spl/spl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -807,20 +807,6 @@ SPL_METHOD(Array, getArrayCopy)
RETURN_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
} /* }}} */

static HashTable *spl_array_get_properties(zval *object) /* {{{ */
{
spl_array_object *intern = Z_SPLARRAY_P(object);

if (intern->ar_flags & SPL_ARRAY_STD_PROP_LIST) {
if (!intern->std.properties) {
rebuild_object_properties(&intern->std);
}
return intern->std.properties;
}

return spl_array_get_hash_table(intern);
} /* }}} */

static HashTable* spl_array_get_debug_info(zval *obj, int *is_temp) /* {{{ */
{
zval *storage;
Expand Down Expand Up @@ -949,6 +935,18 @@ static int spl_array_compare_objects(zval *o1, zval *o2) /* {{{ */
return result;
} /* }}} */

static int spl_array_cast_object(zval *obj, zval *result, int type) /* {{{ */
{
spl_array_object *intern = Z_SPLARRAY_P(obj);

if (type != IS_ARRAY || (intern->ar_flags & SPL_ARRAY_STD_PROP_LIST)) {
return zend_std_cast_object_tostring(obj, result, type);
}

ZVAL_ARR(result, zend_array_dup(spl_array_get_hash_table(intern)));
return SUCCESS;
} /* }}} */

static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
{
zend_string *string_key;
Expand Down Expand Up @@ -2013,7 +2011,6 @@ PHP_MINIT_FUNCTION(spl_array)
spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;

spl_handler_ArrayObject.get_properties = spl_array_get_properties;
spl_handler_ArrayObject.get_debug_info = spl_array_get_debug_info;
spl_handler_ArrayObject.get_gc = spl_array_get_gc;
spl_handler_ArrayObject.read_property = spl_array_read_property;
Expand All @@ -2023,6 +2020,7 @@ PHP_MINIT_FUNCTION(spl_array)
spl_handler_ArrayObject.unset_property = spl_array_unset_property;

spl_handler_ArrayObject.compare_objects = spl_array_compare_objects;
spl_handler_ArrayObject.cast_object = spl_array_cast_object;
spl_handler_ArrayObject.dtor_obj = zend_objects_destroy_object;
spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;

Expand Down
30 changes: 30 additions & 0 deletions ext/spl/tests/ArrayObject_get_object_vars.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
get_object_vars() on ArrayObject works on the properties of the ArrayObject itself
--FILE--
<?php

class Test {
public $test;
public $test2;
}

class AO extends ArrayObject {
private $test;

public function getObjectVars() {
return get_object_vars($this);
}
}

$ao = new AO(new Test);
var_dump(get_object_vars($ao));
var_dump($ao->getObjectVars());

?>
--EXPECT--
array(0) {
}
array(1) {
["test"]=>
NULL
}
72 changes: 44 additions & 28 deletions ext/spl/tests/array_017.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,17 @@ array(3) {
["Flags"]=>
int(0)
["OVars"]=>
array(3) {
[0]=>
int(1)
["a"]=>
int(25)
array(5) {
["pub1"]=>
int(42)
int(1)
["pro1"]=>
int(2)
["pri1"]=>
int(3)
["imp1"]=>
int(4)
["dyn1"]=>
int(5)
}
["$this"]=>
object(ArrayObjectEx)#%d (6) {
Expand Down Expand Up @@ -178,13 +182,17 @@ array(3) {
["Flags"]=>
int(0)
["OVars"]=>
array(3) {
[0]=>
array(5) {
["pub2"]=>
int(1)
["a"]=>
int(25)
["pub1"]=>
int(42)
["pro2"]=>
int(2)
["pri2"]=>
int(3)
["imp2"]=>
int(4)
["dyn2"]=>
int(5)
}
["$this"]=>
object(ArrayIteratorEx)#%d (6) {
Expand Down Expand Up @@ -242,13 +250,17 @@ array(3) {
["Flags"]=>
int(0)
["OVars"]=>
array(3) {
[0]=>
array(5) {
["pub2"]=>
int(1)
["a"]=>
int(25)
["pub1"]=>
int(42)
["pro2"]=>
int(2)
["pri2"]=>
int(3)
["imp2"]=>
int(4)
["dyn2"]=>
int(5)
}
["$this"]=>
object(ArrayIteratorEx)#%d (6) {
Expand Down Expand Up @@ -541,14 +553,16 @@ array(3) {
["Flags"]=>
int(0)
["OVars"]=>
array(4) {
["pub1"]=>
array(5) {
["pub2"]=>
int(1)
["pro1"]=>
["pro2"]=>
int(2)
["imp1"]=>
["pri2"]=>
int(3)
["imp2"]=>
int(4)
["dyn1"]=>
["dyn2"]=>
int(5)
}
["$this"]=>
Expand Down Expand Up @@ -598,14 +612,16 @@ array(3) {
["Flags"]=>
int(0)
["OVars"]=>
array(4) {
["pub1"]=>
array(5) {
["pub2"]=>
int(1)
["pro1"]=>
["pro2"]=>
int(2)
["imp1"]=>
["pri2"]=>
int(3)
["imp2"]=>
int(4)
["dyn1"]=>
["dyn2"]=>
int(5)
}
["$this"]=>
Expand Down
4 changes: 2 additions & 2 deletions ext/spl/tests/bug61347.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var_dump(isset($b['no_exists'])); //false
var_dump(empty($b['b'])); //true
var_dump(empty($b[37])); //true

var_dump(array_key_exists('b', $b)); //true
var_dump(array_key_exists('b', $b)); //false
var_dump($b['b']);

$a = array('b' => '', 37 => false);
Expand All @@ -31,7 +31,7 @@ bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
NULL
bool(true)
bool(true)
Expand Down
2 changes: 0 additions & 2 deletions ext/standard/tests/general_functions/bug47027.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ var_export ($ao);
?>
--EXPECT--
ArrayObject::__set_state(array(
2 => 'foo',
'bar' => 'baz',
))
13 changes: 0 additions & 13 deletions tests/classes/arrayobject_001.phpt

This file was deleted.

0