How to access class members as array indexes in PHP ?
Last Updated :
24 Aug, 2022
In object-oriented programming languages like C++, Java, PHP, etc class members can be accessed by either “.” or “->” operator along with the object for non-static members and class name for static members as per languages.
To access the class members as array indexes in PHP, the get_object_vars() method can be used.
The get_object_vars() function is used to get the properties of the given object. When an object is made, it has some properties. An associative array of properties of the mentioned object is returned by the function. But if there is no property of the object, then it returns NULL. Gets the accessible non-static properties of the given object according to scope.
Note: The get_object_vars() also returns nested object values in the array.
Syntax:
array get_object_vars(object $object)
Parameters:
- $object: An object instance.
Return value:
- Returns an associative array of defined object accessible non-static properties.
Example 1: This code demonstrates the simple class with the static and non-static members.
PHP
<?php
class Geeks {
private $privateMember ;
public $publicMemberWithDefaultValue = 1;
public $publicMemberWithNonDefaultValue ;
static $staticMember ;
public function dumpAllMembers() {
var_dump(get_object_vars( $this ));
}
}
$testObj = new Geeks;
print_r( "\n Dump of object:\n" );
var_dump(( $testObj ));
print_r( "\n Dump array from get_object_vars()"
. "with object:\n" );
var_dump(get_object_vars( $testObj ));
print_r( "\n Dump array from get_object_vars()"
. " from class itself:\n" );
$testObj ->dumpAllMembers();
?>
|
Output
Dump of object:
object(Geeks)#1 (3) {
["privateMember":"Geeks":private]=>
NULL
["publicMemberWithDefaultValue"]=>
int(1)
["publicMemberWithNonDefaultValue"]=>
NULL
}
Dump array from get_object_vars() with object:
array(2) {
["publicMemberWithDefaultValue"]=>
int(1)
["publicMemberWithNonDefaultValue"]=>
NULL
}
Dump array from get_object_vars() from class itself:
array(3) {
["privateMember"]=>
NULL
["publicMemberWithDefaultValue"]=>
int(1)
["publicMemberWithNonDefaultValue"]=>
NULL
}
Example 2: This code demonstrates a nested class showing inheritance.
PHP
<?php
Class Geeks {
private $geeksPrivateMember ;
public $geeksPublicMember ;
}
class AnotherGeeks {
private $privateMember ;
public $publicMemberWithDefaultValue = 1;
public $publicMemberWithNonDefaultValue ;
public $anotherClass ;
public function dumpAllMembers() {
var_dump(get_object_vars( $this ));
}
}
$testObj = new AnotherGeeks;
$nestedObj = new Geeks;
$testObj ->anotherClass = $nestedObj ;
print_r( "\n Dump of object:\n" );
var_dump(( $testObj ));
print_r( "\n Dump array from get_object_vars()"
. "with object:\n" );
var_dump(get_object_vars( $testObj ));
print_r( "\n Dump array from get_object_vars()"
. "from class itself:\n" );
$testObj ->dumpAllMembers();
?>
|
Output
Dump of object:
object(AnotherGeeks)#1 (4) {
["privateMember":"AnotherGeeks":private]=>
NULL
["publicMemberWithDefaultValue"]=>
int(1)
["publicMemberWithNonDefaultValue"]=>
NULL
["anotherClass"]=>
object(Geeks)#2 (2) {
["geeksPrivateMember":"Geeks":private]=>
NULL
["geeksPublicMember"]=>
NULL
}
}
Dump array from get_object_vars() with object:
array(3) {
["publicMemberWithDefaultValue"]=>
int(1)
["publicMemberWithNonDefaultValue"]=>
NULL
["anotherClass"]=>
object(Geeks)#2 (2) {
["geeksPrivateMember":"Geeks":private]=>
NULL
["geeksPublicMember"]=>
NULL
}
}
Dump array from get_object_vars() from class itself:
array(4) {
["privateMember"]=>
NULL
["publicMemberWithDefaultValue"]=>
int(1)
["publicMemberWithNonDefaultValue"]=>
NULL
["anotherClass"]=>
object(Geeks)#2 (2) {
["geeksPrivateMember":"Geeks":private]=>
NULL
["geeksPublicMember"]=>
NULL
}
}
Share your thoughts in the comments
Please Login to comment...