note 102349 deleted from class.mongo by bjori

From: Date: Sat, 01 Sep 2012 15:32:56 +0000
Subject: note 102349 deleted from class.mongo by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190404@lists.php.net to get a copy of this message
Note Submitter: Fausto Vanin @faustovanin ---- For those who are concerned on parsing JSON associative arrays from queries, this class could be useful. You just have to extend it and call parent constructor and it gets the job done. It automatically initializes all your object attributes getting values from the array. <?php #doc # classname: MongoClass # scope: PUBLIC # #/doc class MongoClass { # internal variables protected $id; # Constructor function __construct ($attList = array()) { $reflection = new ReflectionObject($this); foreach ($attList as $attName => $attValue) { $attObj = $reflection->getProperty($attName); $attObj->setAccessible(true); $attObj->setValue($this, $attValue); } } ### } ### class A extends MongoClass { private $name; private $value; private $weight; public function __construct($attList) { parent::__construct($attList); } } $attList = array( "name" => "Beer", "value" => "Delicious", "weight" => 15.2 ); //This is your JSON object associative aray $a = new A($attList); ?>

« previous php.notes (#190404) next »