Auke van Slooten
auke@muze.nl
www.poefke.nl
var adHoc = { property: 'world', method: function() { return 'hello '+this.property; } }
$adHoc = new class { public $property = 'World'; public function method() { return 'Hello '.$this->property; } }
var adHoc2 = { property: 'Moon' } adHoc2.prototype = adHoc;
adHoc2->method(); // => 'Hello Moon'
<?php $adHoc = new adhoc([ 'property' => 'World', 'method' => function() { return 'Hello '.$this->property; } ]);
<?php class addHoc() { public function __construct($props) { foreach($props as $name => $value) { $this->{$name} = $value; } } }
Method 'method' not found.
Properties != Methods
:-(
<?php class addHoc() { public function __call($name, $params) { return call_user_func_array( $this->{$name}, $params ); } }
"Hello " :-(
<?php
class addHoc() {
public function __construct($props) {
foreach($props as $name => $value) {
if ( $value instanceof \Closure ) {
\Closure::bind( $value, $this );
}
$this->{$name} = $value;
}
}
}
"Hello World"