Does ReflectionMethod::setAccessible() do anything?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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();

    Fatal error: Call to protected method Foo::bar() from context '' in php
shell code on line 1

I've tried creating the object first, getting to the ReflectionMethod via
ReflectionClass::getMethod(), and making a public method inaccessible, but
all fail. Has anyone used this feature?

The reason I want to do this, in case anyone can suggest a work-around, is
to allow direct unit testing of protected/private methods in classes. What I
do now is have a function that eval()s a dynamic subclass with a __call()
method that proxies to protected methods. This doesn't allow testing private
methods, and it breaks if the class under test has a private/protected
constructor. I can solve the second problem with some further hacking, but
there's nothing I can do about exposing private methods temporarily.
setAccessible() seems perfectly designed to do what I need.

Thanks,
David

[1] http://php.net/manual/en/reflectionmethod.setaccessible.php

--
David Harkness
Senior Software Engineer
High Gear Media

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux