note 102349 added to class.mongo

From: Date: Thu, 10 Feb 2011 03:31:15 +0000
Subject: note 102349 added to class.mongo
Groups: php.notes 
Request: Send a blank email to php-notes+get-176832@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 187.58.245.104 ---- Manual Page -- http://www.php.net/manual/en/class.mongo.php Edit -- https://master.php.net/note/edit/102349 Del: integrated -- https://master.php.net/note/delete/102349/integrated Del: useless -- https://master.php.net/note/delete/102349/useless Del: bad code -- https://master.php.net/note/delete/102349/bad+code Del: spam -- https://master.php.net/note/delete/102349/spam Del: non-english -- https://master.php.net/note/delete/102349/non-english Del: in docs -- https://master.php.net/note/delete/102349/in+docs Del: other reasons-- https://master.php.net/note/delete/102349 Reject -- https://master.php.net/note/reject/102349 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#176832) next »