Created
September 21, 2013 23:58
-
-
Save bwoebi/6655377 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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