php - Object created using reflection and setValue has empty properties -


i'm trying generate object of class bar post data. i'm doing using function found here.

i post data foo.php bar.php. bar.php receives post data , runs static method bar::generate, returns bar object.

the problem is, though function receives correct data , knows set it, returned objects properties empty.

foo.php

<?php     function postarray($array, $destscript){         try {             $ch = curl_init();              if (false === $ch){                 throw new exception('failed initialize');             }              curl_setopt($ch, curlopt_url,$destscript);             curl_setopt($ch, curlopt_post, count($array));             curl_setopt($ch, curlopt_postfields, http_build_query($array));             curl_setopt($ch, curlopt_returntransfer, true);              $content = curl_exec($ch);             return $content;          if (false === $content)             throw new exception(curl_error($ch), curl_errno($ch));          } catch(exception $e) {             trigger_error(sprintf('curl failed error #%d: %s', $e->getcode(), $e->getmessage()),e_user_error);         }        }      echo postarray(array('action' => 'generate', 'first' => 'yes',  'second' => 'no', 'third' => 'maybe'), 'http://herbie.eu/indev/bar.php'); ?> 

bar.php

<?php     class bar{         public $first;         public $second;         public $third;          private function __construct($options){             $this->loadfromarray($options);         }          private function loadfromarray($array) {             $class = new reflectionclass(get_class($this));             $props = $class->getproperties();              foreach($props $p) {                 if (isset($array[$p->getname()])){                     $p->setvalue($this, $array[$p->getname]);                     echo $p->getname()." = ".$array[$p->getname()]."<br>";                 }             }              echo "<br>";         }          static public function generate($options){             try{                 return new bar($options);             }             catch(notfoundexception $unfe){                 echo 'bar::generate failed + '.$unfe;                 return null;             }         }     }      if(!empty($_post['action'])){         if($_post['action'] == "generate"){             $booking = bar::generate($_post);             echo "success ".count($_post)."<br>";             print_r($booking);         }     }     else{         echo "warning_l0: ${_post}['action'] not set";     } ?> 

running foo.php returns,

first = yes second = no third = maybe  success 4 bar object ( [first] => [second] => [third] => ) 

as can see, reflectionclass in loadfromarray knows put where, returned object empty. 4 result of count($_post).


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -