We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<?php // This class abuses php magic methods to create objects that feel like JS objects class DynamicObject { private $properties = array(); public function __construct($assoc = array()) { if ($assoc) $this->add($assoc); } public function __set($name, $value = null) { return !$value && is_array($name) ? $this->add($name) : $this->properties[$name] = $value; } public function __get($prop) { return $this->properties[$prop] ? is_callable($this->properties[$prop]) ? 'function()' : $this->properties[$prop] : false; } public function __call($name, $args) { return $this->properties[$name](implode(',', $args), $this); } private function add($assoc) { foreach ($assoc as $k => $v) $this->properties[$k] = $v; } } /* ----------------------------------------------------------------------------------- TEST ------------------------------------------------------------------------------------ */ $greeting = new DynamicObject(); $greeting->hello = function ($hello = 'world') { return 'Hello, ' . $hello . "!\n"; }; echo $greeting->hello('JavaScript'); /* * should be * Hello, JavaScript! * * but gives a parse error instead */ ?>
The text was updated successfully, but these errors were encountered:
Thanks for spotting that, will have it sorted out soon
Sorry, something went wrong.
No branches or pull requests
The text was updated successfully, but these errors were encountered: