What is PHP get_class_vars() Function?
If you want to get all the properties (variables) of a class, use PHP get_class_vars() function. The function let you know the name as well as the value of each variable.
Syntax:
get_class_vars(class_name)
Parameters:
The Function has 1 parameter which is required-
class_name (Required): It specifies a string which is a class name. (Check example 1). You can use the get_class() function to get the class name from an object too (Check example 2).
Return Values:
The function returns an associative array whose keys are variable names and their values are the values of the variables.
Examples:
Example 1:
<pre>
<?php
class Human {
public $hand = 2;
public $leg = 2;
public $finger = 20;
}
$var_names = get_class_vars('Human');
echo "The class 'Human' has the following variables- <br />";
print_r($var_names);
?>
</pre>
Output:
The class 'Human' has the following variables-
Array
(
[hand] => 2
[leg] => 2
[finger] => 20
)
Example 2:
<pre>
<?php
class Human {
public $hand = 2;
public $leg = 2;
public $finger = 20;
}
$objHuman = new Human();
$var_names = get_class_vars(get_class($objHuman));
echo "The class 'Human' has the following variables- <br />";
print_r($var_names);
?>
</pre>
Output:
The class 'Human' has the following variables-
Array
(
[hand] => 2
[leg] => 2
[finger] => 20
)
Notes on get_class_vars() Function:
- This function only returns public variables. So, private and protected variables are not included. Check following example-
<pre>
<?php
class Human {
public $hand = 2;
private $legs = 2;
protected $tail = "No";
public $eye = 2;
}
$variable_names = get_class_vars('Human');
echo "The class 'Human' has the following variables- <br />";
print_r($variable_names);
?>
</pre>
Output:
The class 'Human' has the following variables-
Array
(
[hand] => 2
[eye] => 2
)
<pre>
<?php
class Human {
function __construct(){
$this -> hand = 4;
}
public $hand = 2;
public $leg = 2;
public $finger = 20;
}
$var_names = get_class_vars('Human');
echo "The class 'Human' has the following variables- <br />";
print_r($var_names);
?>
</pre>
Output:
The class 'Human' has the following variables-
Array
(
[hand] => 2
[leg] => 2
[finger] => 20
)
<pre>
<?php
class Human {
public $hand = 2;
public $leg = 2;
public $finger = 20;
}
$objHuman = new Human();
$objHuman-> hand = 3;
$var_names = get_class_vars('Human');
echo "The class 'Human' has the following variables- <br />";
print_r($var_names);
?>
</pre>
Output:
The class 'Human' has the following variables-
Array
(
[hand] => 2
[leg] => 2
[finger] => 20
)
<pre>
<?php
class Human {
public $hand = 2;
public $leg = 2;
}
class Male extends Human {
public $finger = 20;
}
$vars_names = get_class_vars('Male');
print_r($vars_names);
?>
</pre>
Output:
Array
(
[finger] => 20
[hand] => 2
[leg] => 2
)
<pre>
<?php
class Human {
function __construct(){
echo "The class 'Human' has the following variables- <br />";
print_r(get_class_vars('Human'));
}
public $hand = 2;
private $legs = 2;
protected $tail = "No";
public $eye = 2;
}
$objHuman = new Human();
?>
</pre>
Output:
The class 'Human' has the following variables-
Array
(
[hand] => 2
[legs] => 2
[tail] => No
[eye] => 2
)
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP get_class_vars() Function
get_class_vars() is a class/object type function in PHP. It is a useful function to see the list of variables a class contains.