Re: Does ReflectionMethod::setAccessible() do anything?
| From: | Nathan Nobbe | Date: | Fri, 17 Dec 2010 02:09:34 +0000 |
| Subject: | Re: Does ReflectionMethod::setAccessible() do anything? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-310096@lists.php.net to get a copy of this message | ||
On Thu, Dec 16, 2010 at 6:37 PM, David Harkness
<david.h@highgearmedia.com>wrote:
> According to the manual page for setAccessible() [1] the feature is
> available with 5.3.2, and I'm running
>
> 5.3.2-1ubuntu4.5 with Suhosin-Patch (cli) (built: Sep 17 2010 13:49:46)
>
> so I should be good to go. However, even the simplest test to make a
> protected or private method accessible fails.
>
> php > class Foo { protected function bar() { echo "foobar\n"; } }
> php > $m = new ReflectionMethod('Foo', 'bar');
> php > $m->setAccessible(true);
> php > $foo = new Foo();
> php > $foo->bar();
you just have to invoke the function from the context of the
ReflectionMethod instance
<?php
class Foo { protected function bar() { echo "foobar\n"; } }
$m = new ReflectionMethod('Foo', 'bar');
$m->setAccessible(true);
$m->invokeArgs(new Foo(), array());
?>
-nathan