How to access class members as array indexes in PHP ? Last Updated : 24 Aug, 2022 Comments Improve Suggest changes Like Article Like Report 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; // Dump of object print_r("\n Dump of object:\n"); var_dump(($testObj)); // Dump of array from get_object_vars() // function with object print_r("\n Dump array from get_object_vars()" . "with object:\n"); var_dump(get_object_vars($testObj)); // Dump of array from get_object_vars() // from class itself 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 to be used as nested class Class Geeks { private $geeksPrivateMember; public $geeksPublicMember; } // Main class for example class AnotherGeeks { private $privateMember; public $publicMemberWithDefaultValue = 1; public $publicMemberWithNonDefaultValue; public $anotherClass; public function dumpAllMembers() { var_dump(get_object_vars($this)); } } // Main test object $testObj = new AnotherGeeks; // Nested object $nestedObj = new Geeks; $testObj->anotherClass = $nestedObj; // Dump of object print_r("\n Dump of object:\n"); var_dump(($testObj)); // Dump of array from get_object_vars() // with object print_r("\n Dump array from get_object_vars()" . "with object:\n"); var_dump(get_object_vars($testObj)); // Dump of array from get_object_vars() // from class itself 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 } } Comment More infoAdvertise with us Next Article How to access class members as array indexes in PHP ? S Shivam.Pradhan Follow Improve Article Tags : Web Technologies PHP PHP-OOP PHP-Questions Similar Reads How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar 6 min read How to load classes in PHP ? PHP load classes are used for declaring its object etc. in object oriented applications. PHP parser loads it automatically, if it is registered with spl_autoload_register() function. PHP parser gets the least chance to load class/interface before emitting an error. Syntax: spl_autoload_register(func 2 min read How to Create an Object Without Class in PHP ? In PHP, creating an object without a class refers to creating an instance of the stdClass, a built-in PHP class for creating generic objects. It allows developers to instantiate objects without explicitly defining a custom class, often used for dynamic data storage.In this article, we will create an 3 min read How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using 2 min read How to Access and Print Matrix Element at Specific Position in PHP ? Accessing and printing elements at specific positions within a matrix is a fundamental operation in PHP when dealing with two-dimensional arrays. A matrix in PHP can be represented as an array of arrays, where each sub-array represents a row in the matrix. This article explores various approaches to 3 min read Associative Arrays in PHP An associative array in PHP is a special array where each item has a name or label instead of just a number. Usually, arrays use numbers to find things. For example, the first item is at position 0, the second is 1, and so on. But in an associative array, we use words or names to find things. These 4 min read How to Loop Through an Array using a foreach Loop in PHP? Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iteratin 2 min read How to check foreach Loop Key Value in PHP ? In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such asTable of ContentUsing the for-each loop with simple valuesUsing the foreach loop with Key-Value pairsUsing array_keys Function to Access Keys and ValuesUsing array_walk Function for IterationUs 3 min read PHP iterator_to_array() Function The iterator_to_array() function is an inbuilt function in PHP that is used to copy the iterator object (e.g. objects implementing the Iterator or IteratorAggregate interface) into an array. An iterator is an object that allows you to loop through a set of values one at a time without knowing the un 3 min read Php Classes and Objects In PHP, Object-Oriented Programming (OOP) makes it easier to organize and reuse code. The two fundamental building blocks in OOP are classes and objects.PHP ClassesA class in PHP is a blueprint for creating objects. It defines the properties (variables) and methods (functions) that the objects creat 4 min read Like