PHP | get_object_vars() Function
Last Updated :
01 Oct, 2021
The get_object_vars() function is an inbuilt function in PHP that 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.
Syntax:
get_object_vars( $object )
Parameters: This function accepts a single parameter as mentioned above and described below:
$object: This parameter holds the object of an instance.
Return Value: This method returns an associative array object accessible non-static properties for the specified object in scope.
Below programs illustrate the get_object_vars() function in PHP:
Program 1:
PHP
<?php
class gfg {
private $geeks = 0.02;
public $for = 1;
public $Geeks = "php" ;
private $GEEKS ;
static $e ;
public function example() {
var_dump(get_object_vars( $this ));
}
}
$example = new gfg;
var_dump(get_object_vars( $example ));
$example ->example();
?>
|
Output:
array(2) {
["for"]=>
int(1)
["Geeks"]=>
string(3) "php"
}
array(4) {
["geeks"]=>
float(0.02)
["for"]=>
int(1)
["Geeks"]=>
string(3) "php"
["GEEKS"]=>
NULL
}
Program 2:
PHP
<?php
class coordinate {
var $x ;
var $y ;
var $z ;
var $labels ;
function coordinate( $x , $y , $z ) {
$this ->x = $x ;
$this ->y = $y ;
$this ->z = $z ;
}
function to_set( $labels ) {
$this ->labels = $labels ;
}
}
$point1 = new coordinate(0.1, 0.2, 0.3);
print_r(get_object_vars( $point1 ));
$point1 ->to_set( "point 1" );
print_r(get_object_vars( $point1 ));
?>
|
Output:
Array
(
[x] => 0.1
[y] => 0.2
[z] => 0.3
[labels] =>
)
Array
(
[x] => 0.1
[y] => 0.2
[z] => 0.3
[labels] => point 1
)
Reference: https://www.php.net/manual/en/function.get-object-vars.php
Share your thoughts in the comments
Please Login to comment...