- 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}()");
- }
- }
- }
- $obj = new stdObject();
- $obj->navn = "John";
- $obj->efternavn = "Doe";
- $obj->alder = 20;
- $obj->adresse = null;
- $obj->getInfo = function($stdObject) { // $stdObject referred to this object (stdObject).
- echo $stdObject->navn . " " . $stdObject->efternavn . " er " . $stdObject->alder . " år gammel. Og lever på " . $stdObject->adresse;
- };