Saturday, November 16, 2024
Google search engine
HomeLanguagesHow to access class members as array indexes in PHP ?

How to access class members as array indexes in PHP ?

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 $neveropenPrivateMember;
    public $neveropenPublicMember;
}
  
// 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) {
    ["neveropenPrivateMember":"Geeks":private]=>
    NULL
    ["neveropenPublicMember"]=>
    NULL
  }
}

 Dump array from get_object_vars() with object:
array(3) {
  ["publicMemberWithDefaultValue"]=>
  int(1)
  ["publicMemberWithNonDefaultValue"]=>
  NULL
  ["anotherClass"]=>
  object(Geeks)#2 (2) {
    ["neveropenPrivateMember":"Geeks":private]=>
    NULL
    ["neveropenPublicMember"]=>
    NULL
  }
}

 Dump array from get_object_vars() from class itself:
array(4) {
  ["privateMember"]=>
  NULL
  ["publicMemberWithDefaultValue"]=>
  int(1)
  ["publicMemberWithNonDefaultValue"]=>
  NULL
  ["anotherClass"]=>
  object(Geeks)#2 (2) {
    ["neveropenPrivateMember":"Geeks":private]=>
    NULL
    ["neveropenPublicMember"]=>
    NULL
  }
}

RELATED ARTICLES

Most Popular

Recent Comments