public function LinkObject($Object, $ConstsWithPrefix = true, $MethodsWithPrefix = false, $PropertiesWithPrefix = false) { # Reflection $Class = new ReflectionClass($Object); $ClassName = $Class->GetShortName(); $Constants = $Class->GetConstants(); $Methods = $Class->GetMethods(ReflectionMethod::IS_PUBLIC); $Properties = $Class->GetProperties(ReflectionProperty::IS_PUBLIC); # Prefixes $ClassPrefix = $ClassName . '_'; $ConstsPrefix = $ConstsWithPrefix ? strtoupper($ClassPrefix) : null; $MethodsPrefix = $MethodsWithPrefix ? $ClassPrefix : null; $PropertiesPrefix = $PropertiesWithPrefix ? $ClassPrefix : null; # Consts $this->AssignVariables($Constants, $ConstsPrefix); # Callbacks /** * If you can do this * In one line * Without using aux vars * Without repeating code * Without doing things like: asd($a = dsa(), ...) * * YOU'RE A GOD. */ // Convert ReflectionMethod's into array(MethodName => Callback, [...]) $Methods = array_map(function ($Method) { return $Method->name; }, $Methods); $Methods = array_combine($Methods, array_map(function ($Method) use($Object) { return array($Object, $Method); }, $Methods)); $Methods = array_filter($Methods, function ($Method) { return strpos($Method, '__') !== 0; }, ARRAY_FILTER_USE_KEY); $this->RegisterCallbacks($Methods, $MethodsPrefix); # Vars // Convert ReflectionProperty's into array(PropertyName => Value, [...]) $Properties = array_map(function ($Property) { return $Property->name; }, $Properties); $Properties = array_combine($Properties, array_map(function ($Property) use($Object) { return $Object->{$Property}; }, $Properties)); $this->AssignVariables($Properties); return true; // Return consts && methods && properties (all of it) }
<?php class ancester { public $ancester = 0; function __construct() { return $this->ancester; } } class foo extends ancester { public $bar = "1"; function __construct() { return $this->bar; } } $r = new ReflectionClass('foo'); foreach ($r->GetProperties() as $p) { echo $p->getName() . " " . $p->getDeclaringClass()->getName() . "\n"; }