note 107519 added to function.get-class-vars

From: Date: Tue, 14 Feb 2012 21:39:31 +0000
Subject: note 107519 added to function.get-class-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-186138@lists.php.net to get a copy of this message
get_class_vars_assoc() - Returns an associative array with name of (parent) class(es) as key(s) and corresponding class vars as sub-arrays. My boilerplate for some crude O/R mapping. Note: vars re-defined in sub-classes are ignored. <?php class GrandClass { public $grandVar; public $in_grand_and_parent; public $in_grand_and_child; public static function load() { print_r(self::get_class_vars_assoc()); } protected static function get_class_vars_assoc() { $called = get_called_class(); //echo "called: $called \n"; $classVars[$called] = array_keys(get_class_vars($called)); $parent = get_parent_class($called); while ( $parent !== FALSE ) { //echo "parent: $parent \n"; $classVars[$parent] = array_keys(get_class_vars($parent)); $classVars[$called] = array_diff($classVars[$called], $classVars[$parent]); if ( isset($prevParentVars) ) { $prevParentVars = array_diff($prevParentVars, $classVars[$parent]); } $prevParentVars = &$classVars[$parent]; $parent = get_parent_class($parent); } return $classVars; } } class ParentClass extends GrandClass { public $in_grand_and_parent; public $parentVar; public $in_parent_and_child; } class ChildClass extends ParentClass { public $in_grand_and_child; public $in_parent_and_child; public $childVar; } ChildClass::load(); ?> Array ( [ChildClass] => Array ( [2] => childVar ) [ParentClass] => Array ( [1] => parentVar [2] => in_parent_and_child ) [GrandClass] => Array ( [0] => grandVar [1] => in_grand_and_parent [2] => in_grand_and_child ) ) ---- Server IP: 212.63.222.76 Probable Submitter: 94.254.92.182 ---- Manual Page -- http://www.php.net/manual/en/function.get-class-vars.php Edit -- https://master.php.net/note/edit/107519 Del: integrated -- https://master.php.net/note/delete/107519/integrated Del: useless -- https://master.php.net/note/delete/107519/useless Del: bad code -- https://master.php.net/note/delete/107519/bad+code Del: spam -- https://master.php.net/note/delete/107519/spam Del: non-english -- https://master.php.net/note/delete/107519/non-english Del: in docs -- https://master.php.net/note/delete/107519/in+docs Del: other reasons-- https://master.php.net/note/delete/107519 Reject -- https://master.php.net/note/reject/107519 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186138) next »