note 118872 added to class.arrayobject

From: Date: Fri, 19 Feb 2016 13:36:52 +0000
Subject: note 118872 added to class.arrayobject
Groups: php.notes 
Request: Send a blank email to php-notes+get-204994@lists.php.net to get a copy of this message
<?php class Prototype extends ArrayObject { private $___class = null; public function __get($key) { return $this[$key]; } public function __set($key, $value) { $this[$key] = $value; } public function __call($key, $args) { if(is_object($this->___class) && is_callable([$this->___class, $key])){ return call_user_func_array([$this->___class, $key],$args); } return is_callable($c = $this->__get($key)) ? call_user_func_array($c, $args) : null; } public function importObj($class, $array = []){ $this->___class = $class; if(count($array) > 0){ $this->import($array); } return $this; } public function import($input) { $this->exchangeArray($input); return $this; } public function export() { return $this->objectToArray($this->getArrayCopy()); } public function objectToArray ($object) { $o = []; foreach ($object as $key => $value) { $o[$key] = is_object($value) ? (array) $value: $value; } return $o; } } class user{ public $name = 'Mahmoud Elnezamy'; public function getName(){ return 'You Name is ' . $this->name; } } //usage you can import object with some array $add = ['age' => '27', 'country' => 'Egypt']; $user = new user; $Prototype = new Prototype; $Prototype->importObj($user, $add); //print_r($Prototype); echo $Prototype->getName().' '; echo $Prototype->age.' '; echo $Prototype->country; ---- Server IP: 72.52.91.14 Probable Submitter: 196.218.141.212 ---- Manual Page -- http://php.net/manual/en/class.arrayobject.php Edit -- https://master.php.net/note/edit/118872 Del: integrated -- https://master.php.net/note/delete/118872/integrated Del: useless -- https://master.php.net/note/delete/118872/useless Del: bad code -- https://master.php.net/note/delete/118872/bad+code Del: spam -- https://master.php.net/note/delete/118872/spam Del: non-english -- https://master.php.net/note/delete/118872/non-english Del: in docs -- https://master.php.net/note/delete/118872/in+docs Del: other reasons-- https://master.php.net/note/delete/118872 Reject -- https://master.php.net/note/reject/118872 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204994) next »