Skip to content

Instantly share code, notes, and snippets.

@bwoebi
Created September 21, 2013 23:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwoebi/6655377 to your computer and use it in GitHub Desktop.
Save bwoebi/6655377 to your computer and use it in GitHub Desktop.
<?php
const CLASS_PUBLIC = "";
const CLASS_PROTECTED = "\0*\0";
const CLASS_PRIVATE = "\0stdClass\0";
function create_class ($class) {
$class = (object)$class;
foreach ($class as $prop)
if ($prop instanceof Closure)
$prop->bindTo($class);
return $class;
}
// Usage:
$class = create_class([
CLASS_PUBLIC."public_prop" => 123,
CLASS_PROTECTED."protected_prop" => 456,
CLASS_PRIVATE."private_prop" => 789,
CLASS_PUBLIC."public_method" => function () { /* ... */ },
CLASS_PROTECTED."protected_method" => function () { /* ... */ },
CLASS_PRIVATE."private_method" => function () { /* ... */ },
]);
// Output:
var_dump($class);
/*
object(stdClass)#4 (6) {
["public_prop"]=>
int(123)
["protected_prop":protected]=>
int(456)
["private_prop":"stdClass":private]=>
int(789)
["public_method"]=>
object(Closure)#1 (0) {
}
["protected_method":protected]=>
object(Closure)#2 (0) {
}
["private_method":"stdClass":private]=>
object(Closure)#3 (0) {
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment