|
get_class_vars
Get the default properties of the class
(PHP 4, PHP 5)
Example 376. get_class_vars() example<?php The above example will output: // Before PHP 4.2.0 Related Examples ( Source code ) » get_class_vars Examples ( Source code ) » get_class_vars Code Examples / Notes » get_class_varsphpnet
This is one of the best php functions. Look at what you can do class Object { var $updtFields;//keep track of affected values function Object($record="") { if (is_array($record)) { $this->updtFields = array(); foreach(array_keys(get_class_vars(get_class($this))) as $k) if (isset($record[$k])) { $this->$k = $record[$k]; $this->updtFields[] = $k; } } }//end of arrayToObject function toDebug($nl=' ') { foreach(array_keys(get_class_vars(get_class($this))) as $k) echo "$k = [" . $this->$k . "]{$nl}"; }//end of toDebug } Now you can do really cool things. If you have a form like <form action="" method="post"> <input type="text" name="name" /> <input type="text" name="phone" /> <input type="submit" /> </form> and you define your class like this class Person extends Object{ var $name; //same same as in the form var $phone; } when you submmit the form, you can get the data like $person = new Person($_POST); //everything in just one line,cool!! Also if you use pear db or adodb when you get data from the database you can do the same thing except use the $row that you get from the database. Remember to ask the result is associative mode. This is my core Object for everthing I do and it works great. alan_k
in PHP5 to get all the vars (including private etc.) use: $reflection = new ReflectionClass($class); $defaults = $reflection->getdefaultProperties(); rec
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 bernd
If you assign a constant value using the self-scope by default to a variable, get_class_vars() will result in a FATAL error. Example: <?PHP class Foo { const Bar = "error"; public $Foo = self::Bar; } print_r(get_class_vars("Foo")); ?> ... but using "Foo::Bar" instead "self::Bar" will work ;) gizmobits
I wanted a simple ToString() function that was automatic and class independent. I wanted to dump it into any of several classes and get values quickly. I wanted to leave it there so I could customize it for each class, so an outside function wasn't suitable. I came up with this and thought it might be useful. Have fun! <?php function ToString () { $s = ""; $s .= "<table>\n"; $s .= "<tr><td colspan=2><hr></td></tr>\n"; foreach (get_class_vars(get_class($this)) as $name => $value) { $s .= "<tr><td>$name:</td><td>" . $this->$name . "</td></tr>\n"; } $s .= "<tr><td colspan=2><hr></td></tr>\n"; $s .= "</table>\n"; return $s; } ?> php dot net
Contrary to multiple comments throughout the manual, get_class_vars() performed within a class can access any public, protected, and private members. <?php class Foo { public $x; protected $y; private $z; public function __sleep() { return( get_class_vars( __CLASS__ ) ); } } ?> works fine (returns x, y, & z). However, given the same class as above, <?php print_r( get_class_vars( "Foo" ) ); ?> will NOT return x, y, & z. Instead it will only return the public members (in our case, z). |