Friday, October 10, 2025
HomeLanguagesPHP get_class_vars() Function

PHP get_class_vars() Function

The get_class_vars() function is an inbuilt function in PHP which is used to get the default properties of a class.

Syntax:

array get_class_vars(string $class)

Parameters: This function accepts one parameter that is described below:

  • $class: This parameter specifies the name of the class.

Return Value: This function returns an associative array like “var name => value” if the get_class_vars() function is successful otherwise it will return “false”.

Example 1: This example demonstrates the basic use of the get_class_vars() function.

PHP




<?php
    class neveropen{
          var $var1 = ""
          var $var2 = "Geeks" ;
          
          function __construct(){
               $this->var1 ="Articles" ;
               $this->var2 = "neveropen" ;
               return true ;
          }
    }
      
    $ob = new neveropen() ;
    $array = get_class_vars(get_class($ob));
  
    foreach($array as $name => $value){
          echo "$name : $value\n";
    }
?>


Output:

var1 : 
var2 : Geeks

Example 2: This is another example that demonstrates the use of the get_class_vars() function.

PHP




<?php
    function format($array) {
        return implode('|', array_keys($array)) . "\r\n";
    }
  
    class TestCase {
        public $a    = 1;
        protected $b = 2;
        private $c   = 3;
  
        public static function expose() {
            echo format(get_class_vars(__CLASS__));
        }
    }
  
    TestCase::expose();
    echo format(get_class_vars('TestCase'));
?>


Output: 

a|b|c
a

Reference: https://www.php.net/manual/en/function.get-class-vars.php

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS