search for in the  
<get_class_methodsget_class>
Last updated: Thu, 19 May 2005

get_class_vars

(PHP 4, PHP 5)

get_class_vars --  Returns an array of default properties of the class

Description

array get_class_vars ( string class_name )

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

Note: Prior to PHP 4.2.0, Uninitialized class variables will not be reported by get_class_vars().

Example 1. get_class_vars() example

<?php

class myclass {

   var
$var1; // this has no default value...
  
var $var2 = "xyz";
   var
$var3 = 100;
   private
$var4; // PHP 5
  
   // constructor
  
function myclass() {
      
// change some properties
      
$this->var1 = "foo";
      
$this->var2 = "bar";
       return
true;
   }

}

$my_class = new myclass();

$class_vars = get_class_vars(get_class($my_class));

foreach (
$class_vars as $name => $value) {
   echo
"$name : $value\n";
}

?>

The above example will output:

// Before PHP 4.2.0
var2 : xyz
var3 : 100

// As of PHP 4.2.0
var1 :
var2 : xyz
var3 : 100

See also get_class_methods(), get_object_vars()



User Contributed Notes
get_class_vars
alan_k at php dot net
21-Jan-2005 09:23
in PHP5 to get all the vars (including private etc.) use:

$reflection = new ReflectionClass($class);
$defaults = $reflection->getdefaultProperties();
rec at NOSPAM dot instantmassacre dot com
23-Jan-2003 06:23
If you want to retrieve the class vars from within the class itself, use $this.

<?php
class Foo {

   var
$a;
   var
$b;
   var
$c;
   var
$d;
   var
$e;

   function
GetClassVars()
   {
       return
array_keys(get_class_vars(get_class($this))); // $this
  
}

}

$Foo = new Foo;

$class_vars = $Foo->GetClassVars();

foreach (
$class_vars as $cvar)
{
   echo
$cvar . "<br />\n";
}
?>

Produces, after PHP 4.2.0, the following:

a
b
c
d
e

<get_class_methodsget_class>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT