note 114443 added to language.oop5

From: Date: Fri, 21 Feb 2014 06:50:12 +0000
Subject: note 114443 added to language.oop5
Groups: php.notes 
Request: Send a blank email to php-notes+get-198579@lists.php.net to get a copy of this message
Here a simple class 'stdObject' that give us the possibility to create dynamic classes and the possibility to add and execute methods thing that 'stdClass' don't let us do. Very useful if you extends it to a controller on MVC Design pattern. Let users create own classes. I have also post this class on http://www.php.net/manual/en/language.types.object.php <?php class stdObject { public function __construct(array $arguments = array()) { if (!empty($arguments)) { foreach ($arguments as $property => $argument) { $this->{$property} = $argument; } } } public function __call($method, $arguments) { $arguments = array_merge(array("stdObject" => $this), $arguments); // Note: method argument 0 will always referred to the main class ($this). if (isset($this->{$method}) && is_callable($this->{$method})) { return call_user_func_array($this->{$method}, $arguments); } else { throw new Exception("Fatal error: Call to undefined method stdObject::{$method}()"); } } } // Usage. $obj = new stdObject(); $obj->name = "Nick"; $obj->surname = "Doe"; $obj->age = 20; $obj->adresse = null; $obj->getInfo = function($stdObject) { // $stdObject referred to this object (stdObject). echo $stdObject->name . " " . $stdObject->surname . " have " . $stdObject->age . " yrs old. And live in " . $stdObject->adresse; }; $func = "setAge"; $obj->{$func} = function($stdObject, $age) { // $age is the first parameter passed when calling this method. $stdObject->age = $age; }; $obj->setAge(24); // Parameter value 24 is passing to the $age argument in method 'setAge()'. // Create dynamic method. Here i'm generating getter and setter dynimically // Beware: Method name are case sensitive. foreach ($obj as $func_name => $value) { if (!$value instanceOf Closure) { $obj->{"set" . ucfirst($func_name)} = function($stdObject, $value) use ($func_name) { // Note: you can also use keyword 'use' to bind parent variables. $stdObject->{$func_name} = $value; }; $obj->{"get" . ucfirst($func_name)} = function($stdObject) use ($func_name) { // Note: you can also use keyword 'use' to bind parent variables. return $stdObject->{$func_name}; }; } } $obj->setName("John"); $obj->setAdresse("Boston"); $obj->getInfo(); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/language.oop5.php Edit -- https://master.php.net/note/edit/114443 Del: integrated -- https://master.php.net/note/delete/114443/integrated Del: useless -- https://master.php.net/note/delete/114443/useless Del: bad code -- https://master.php.net/note/delete/114443/bad+code Del: spam -- https://master.php.net/note/delete/114443/spam Del: non-english -- https://master.php.net/note/delete/114443/non-english Del: in docs -- https://master.php.net/note/delete/114443/in+docs Del: other reasons-- https://master.php.net/note/delete/114443 Reject -- https://master.php.net/note/reject/114443 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198579) next »