php - Extending ArrayObject: Why does get_object_vars return an empty array? -


given following class, why get_object_vars return empty array? happens when i'm extending php's arrayobject in documentation can't manage find out reason behavior.

class test extends arrayobject {     public $foo;     public $bar;      public function setfoobarvalues( array $values )     {         $this->foo  = !empty( $values['foo'] ) ? $values['foo'] : null;         $this->bar  = !empty( $values['bar'] ) ? $values['bar'] : null;     }      public function getarraycopy()     {         return get_object_vars( $this );     } } 

running following code first sets object's values shows get_object_vars not return object's properties.

$object = new test( array( 'lemon' => 1, 'orange' => 2, 'banana' => 3, 'apple' => 4 ) ); $object->setfoobarvalues( array( 'foo' => 'x', 'bar' => 'y' ) ); var_dump( $object->getarraycopy() ); 

expected result:

array(2) {   ["foo"]=>   string(1) "x"   ["bar"]=>   string(1) "y" } 

actual result:

array(0) { } 

while reason not explained in manual object seems handle properties internally. take @ constructor's second parameter int $flags = 0 , 2 flags provided in manual:

  • arrayobject::std_prop_list properties of object have normal functionality when accessed list (var_dump, foreach, etc.).

  • arrayobject::array_as_props entries can accessed properties (read , write).

the constant std_prop_list want use standard property access. supplying constructor constant give result looking for:

$object = new test(     array( 'lemon' => 1, 'orange' => 2, 'banana' => 3, 'apple' => 4 ),     arrayobject::std_prop_list ); $object->setvalues( array( 'foo' => 'x', 'bar' => 'y' ) ); var_dump( $object->getarraycopy() ); 

result:

array(2) {   ["foo"]=>   string(1) "x"   ["bar"]=>   string(1) "y" } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -