php.js icon indicating copy to clipboard operation
php.js copied to clipboard

Parse error: Object [object Object] has no method 'Node_Expr_Closure' in /console.htm on line undefined

Open nfeldman opened this issue 13 years ago • 1 comments

<?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
 */
?>

nfeldman avatar Jul 25 '12 14:07 nfeldman

Thanks for spotting that, will have it sorted out soon

niklasvh avatar Jul 27 '12 06:07 niklasvh