/ Published in: PHP
This is a class/demonstration for creating dynamic properties and storing them in an array
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class AbstractModel { protected function __set($name, $value) { $this->data[$name] = $value; } protected function __get($name) { return $this->data[$name]; } return null; } protected function __isset($name) { } protected function __unset($name) { } public function printOut() { echo "<pre>"; echo "</pre>"; } } class User extends AbstractModel { } // how to use $person = new User(); $person->name = 'John'; $person->age = 26; $person->address = "217 Ziller's road brooklyn NY 11553"; $person->printOut(); $car = new User(); $car->make = 'Honda'; $car->model = 'Civic'; $car->cylinder = 6; $car->transmission = 'Automatic'; $car->year = 2006; $truck = new User(); $truck->make = 'Ford'; $truck->model = 'F-150'; $truck->cylinder = 12; $truck->transmission = 'Manual'; $truck->year = 2006; $person->printOut(); //output User Object ( ( [name] => John [age] => 26 [address] => 217 Ziller's road brooklyn NY 11553 ) ) User Object ( [data:private] => Array ( [name] => John [age] => 26 [address] => 217 Ziller's road brooklyn NY 11553 ( [honda] => User Object ( ( [make] => Honda [model] => Civic [cylinder] => 6 [transmission] => Automatic [year] => 2006 ) ) [Ford] => User Object ( ( [make] => Ford [model] => F-150 [cylinder] => 12 [transmission] => Manual [year] => 2006 ) ) ) ) )