Longhorn PHP 2025 - Speakers and Schedule Announced!

Voting

: max(zero, nine)?
(Example: nine)

The Note You're Voting On

Ashley Dambra
11 years ago
Here a simple class that allow to set anonymous function. It's an optimised class of stdClass.

<?php
class stdObject {
public function
__construct(array $arguments = array()) {
if (!empty(
$arguments)) {
foreach (
$arguments as $property => $argument) {
if (
$argument instanceOf Closure) {
$this->{$property} = $argument;
} else {
$this->{$property} = $argument;
}
}
}
}

public function
__call($method, $arguments) {
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}()");
}
}
}

$person = new stdObject(array(
"name" => "nick",
"age" => 23,
"friends" => array("frank", "sally", "aaron"),
"sayHi" => function() {
return
"Hello there";
}
));

$person->sayHi2 = function() {
return
"Hello there 2";
};

$person->test = function() {
return
"test";
};

var_dump($person->name, $person->test(), $person->sayHi2());
?>

<< Back to user notes page

To Top