get object vars php

get_object_vars

(PHP 4, PHP 5, PHP 7, PHP 8)

get_object_vars — Gets the properties of the given object

Description

Gets the accessible non-static properties of the given object according to scope.

Parameters

An object instance.

Return Values

Returns an associative array of defined object accessible non-static properties for the specified object in scope.

Examples

Example #1 Use of get_object_vars()

The above example will output:

Uninitialized properties are considered inaccessible, and thus will not be included in the array.

See Also

User Contributed Notes 4 notes

You can still cast the object to an array to get all its members and see its visibility. Example:

\n» ;
echo «Using get_object_vars:\n» ;

echo «\n\nUsing array cast:\n» ;

Using get_object_vars:
Array
(
[skin] => 1
)

Using array cast:
Array
(
[skin] => 1
[ * meat] => 2
[ Potatoe roots] => 3
)

As you can see, you can obtain the visibility for each member from this cast. That which seems to be spaces into array keys are ‘\0’ characters, so the general rule to parse keys seems to be:

Public members: member_name
Protected memebers: \0*\0member_name
Private members: \0Class_name\0member_name

I’ve wroten a obj2array function that creates entries without visibility for each key, so you can handle them into the array as it were within the object:

?>

I’ve created also a bless function that works similar to Perl’s bless, so you can further recast the array converting it in an object of an specific class:

\n» ;
echo «Using get_object_vars:\n» ;

echo «\n\nUsing obj2array func:\n» ;

echo «\n\nSetting all members to 0.\n» ;
$Arr [ ‘skin’ ]= 0 ;
$Arr [ ‘meat’ ]= 0 ;
$Arr [ ‘roots’ ]= 0 ;

You can use an anonymous class to return public variables from inside the class:

$test = new Test();
print_r(get_object_vars($test)); // array(«public» => NULL)
print_r($test->getAllVars()); // array(«protected» => NULL, «public» => NULL, «private» => NULL)
print_r($test->getPublicVars()); // array(«public» => NULL)

It seems like there’s no function that determines all the *static* variables of a class.

I’ve come out with this one as I needed it in a project:

When dealing with a very large quantity of objects, it is worth noting that using `get_object_vars()` may drastically increase memory usage.

If instantiated objects only use predefined properties from a class then PHP can use a single hashtable for the class properties, and small memory-efficient arrays for the object properties:

However, if you call `get_object_vars()` on an object like this, then PHP WILL build a hashtable for the individual object. If you have a large quantity of objects, and you call `get_object_vars()` on all of them, then a hashtable will be built for each object, resulting in a lot more memory usage. This can be seen in this bug report: https://bugs.php.net/bug.php?id=79392

The effects of this can be seen in this example:

printMem ( ‘before get_object_vars’ );

printMem ( ‘get_object_vars using clone’ );

// The memory is used even if you do not modify the object.
>

printMem ( ‘get_object_vars direct access’ );
?>

The output of this is:

start: 405704 (0.41 MB)
before get_object_vars: 6512416 (6.51 MB)
get_object_vars using clone: 6033408 (6.03 MB)
get_object_vars direct access: 13553408 (13.55 MB)

In short, if you are using classes to avoid additional memory usage associated with hashtables (like in associative arrays), be aware that `get_object_vars()` will create a hashtable for any object passed to it.

This appears to be present in all versions of PHP; I’ve tested it on PHP 5, 7, and 8.

Quotes are from Nikic’s blog posts on arrays and hashtable memory usage, and Github gist «Why objects (usually) use less memory than arrays in PHP».

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *