note 101585 added to reflection.examples

From: Date: Tue, 28 Dec 2010 10:11:39 +0000
Subject: note 101585 added to reflection.examples
Groups: php.notes 
Request: Send a blank email to php-notes+get-175270@lists.php.net to get a copy of this message
Simple table generation using Reflection: <?php class A { public $name = 'Vasiliy'; public $text = 'Once upon a time'; } class B { public $name = 'Had bought a cat'; public $text = 'named Valentine'; } class grid { public function build( $dataSource, $headers, $fields ) { $result = '<table border="1">'; $result .= $this -> make_head( $headers ); foreach ($dataSource as $source): $class_name = get_class($source); if ( $class_name != FALSE ): $reflector = new ReflectionClass($source); echo 'Class "'. $class_name .'" found.<br />'; $result .= $this -> make_row( $reflector, $fields, $source ); endif; endforeach; $result .= '</table>'; return $result; } private function make_head( $headers ) { $result = '<tr>'; foreach ( $headers as $header): $result .= "<th>$header</th>"; endforeach; $result .= '</tr>'; return $result; } private function make_row( $reflector, $fields, $source ) { $result = '<tr>'; foreach ( $fields as $field ): if ( $reflector -> hasProperty($field) ): $property = $reflector -> getProperty($field); $result .= '<td>'. $property -> getValue($source) .'</td>'; endif; endforeach; $result .= '</tr>'; return $result; } } $A = new A; $B = new B; $C = 'Test'; $dataSource = array( $A, $B, $C ); $headers = array( 'H1', 'H2' ); $fields = array( 'name', 'text' ); $grid = new grid; $table = $grid -> build( $dataSource, $headers, $fields ); echo $table; ?> ---- Server IP: 213.186.192.247 Probable Submitter: 188.163.235.241 ---- Manual Page -- http://www.php.net/manual/en/reflection.examples.php Edit -- https://master.php.net/note/edit/101585 Del: integrated -- https://master.php.net/note/delete/101585/integrated Del: useless -- https://master.php.net/note/delete/101585/useless Del: bad code -- https://master.php.net/note/delete/101585/bad+code Del: spam -- https://master.php.net/note/delete/101585/spam Del: non-english -- https://master.php.net/note/delete/101585/non-english Del: in docs -- https://master.php.net/note/delete/101585/in+docs Del: other reasons-- https://master.php.net/note/delete/101585 Reject -- https://master.php.net/note/reject/101585 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175270) next »