at 2010-04-04
in 5.8-SERIES
by friebe
(0 comments)
The next major XP Framework series, 5.8, now allows invoking protected methods reflectively:
class Invoke extends Object { protected function target() { Console::writeLine('Invoked'); } public function run() { $this->getClass()->getMethod('target')->invoke($this); } public static function main(array $args) { create(new self())->run(); } }In the 5.7-SERIES, this would raise a lang.reflect.TargetInvocationException; whereas now it prints "Invoked" to the console as expected. The same works for (static and non-static) fields as well as constructors seamlessly if inside the same class or a subclass. To invoke protected methos and/or read or write protected fields of any class, use the setAccessible() method provided in the Field, Method and Constructor classes:
public function dump(Generic $instance) { Console::writeLine($instance->getClass(), ' {'); foreach ($instance->getClass()->getFields() as $field) { Console::write(' [', $field->getName(), '] '); Console::writeLine($field->setAccessible(TRUE)->get($instance)); } Console::writeLine('}'); }Note: This feature does not rely on PHP 5.3 but utilizes __get() / __set() magic interceptors
|
Subscribe
You can subscribe to the XP framework's news by using RSS syndication.
CategoriesNews General PHP5 Announcements RFCs Further reading Examples Editorial EASC Experiments Unittests Databases 5.8-SERIES Unicode Language 5.9-SERIES
RelatedFind related articles by a search for «Protected».
|