PHP | get_class_vars() Function

PHP get_class_vars() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP get_class_vars() Function.

The get_class_vars() function in PHP gets the default properties of the given class. It returns an associative array of the default public properties of the class.

Syntax

Following below is the syntax to use this function -

get_class_vars ( $class_name );


Parameter Details

Sr.NoParameter & Description
1

class_name(Required)

The class name.


Return Value

This PHP function returns an associative array of default public properties of the class. The resulting array elements are in the form of varname=>value.

Example

Try out the below example -

<?php
   class helloworld {
      var $var1;
      var $var2 = "xyz";
      var $var3 = 100;
      private $var4; // PHP 5
      
      function helloworld() {
         $this->var1 = "foo";
         $this->var2 = "bar";
         return true;
      }
   }
	
   $hello_class = new helloworld();
   $class_vars = get_class_vars(get_class($hello_class));
   
   foreach ($class_vars as $name => $value) {
      echo "$name : $value \n";
   }
?> 

Output

When the above code is executed, it will produce the following result -

var1 :
var2 : xyz
var3 : 100


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP get_class() Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.