irwan

get PHP object variables as an array

Nov 15th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.38 KB | None | 0 0
  1. <?php
  2.  
  3. class toonsObj{
  4.     var $mickey = 'mouse';
  5.     var $donald = 'duck';
  6.     var $pluto = 'dog';
  7. }
  8.  
  9. //instantiate the variable
  10. $toons = new toonsObj;
  11.  
  12. //add a new object variable
  13. $toons->porky = 'pig';
  14.  
  15. //return an associative array of object variable names
  16. $vars = get_object_vars($toons);
  17.  
  18. foreach ($vars as $name=>$val) {
  19.     echo "$name is a $val <BR/>";
  20. }
  21. ?>
  22.  
Advertisement
Add Comment
Please, Sign In to add comment