Wednesday, November 20, 2024
Google search engine
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

Recent Comments