note 116800 deleted from language.oop5 by peehaa
| From: | peehaa@php.net | Date: | Sun, 28 Apr 2019 10:02:52 +0000 |
| Subject: | note 116800 deleted from language.oop5 by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210733@lists.php.net to get a copy of this message | ||
Note Submitter: 0x174[d0t]14[at]gmail[d0t]com
----
A little trick for create an anonymous object (exactly an half-anonymous object :D aka
"_") who can handle methods.
<?php
class _
{
public function __construct( array $cfg){
foreach($cfg as $k=>$v){
$this->{$k}=$v;
}
}
public function __call( $fn, array $args){
if(isset($this->{$fn})){
array_unshift($args, $this);
call_user_func_array( $this->{$fn}, $args);
}
}
}
$o = new _(array(
"color"=>"red",
"run"=>function($this){
echo "My color is : ".$this->color;
}
));
$o->run();
?>