Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

get_class_vars
variables
php
communitycreator

What is the get_class_vars in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The get_class_vars method can be used to get the default value of the class properties available in the current scope.

get_class_vars(string $class): array

This method takes the class name as an argument.

This method returns an associate array as a result. The key is the property name and the value is the default property value.

Code

<?php
class MyClass {
var $a; // no value
var $b = 10;
var $c = "100";
private $d = "private";
protected $e = "protected";
// constructor
function __construct() {
// set value for the property a
$this->a = 0;
return true;
}
function printClassVar(){
echo "From inside class scope \n";
print_r(get_class_vars('MyClass'));
}
}
// normal scope
echo "From outer scope \n";
print_r(get_class_vars('MyClass'));
// class scope
$obj = new MyClass();
$obj->printClassVar();
?>

In the code above,

  • We have created a class, MyClass, which has the public and private protected properties as well as a method to call the get_class_vars method.

  • First, we called the get_class_vars method outside the function. In this case, we will get the public properties with the default value of the MyClass class.

  • Then we called the printClassVar method of the MyClass class. This method will call the get_class_vars method. Now the scope is the MyClass class scope so that all the public, private, and protected properties with default values will be returned.

RELATED TAGS

get_class_vars
variables
php
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring