die musterdenker

Changing scope in PHP for unit testing

Tags: , , ,

Mathias Krieck recently publish an easy way to change scope of methods and variables.

The main use is for unit testing (e.g PHPUnit) when you need to access protected or private variables / methods. With his “Super Proxy” you do:

$oInstance = SuperProxy::getInstance("CLASSNAME", $aParams);

Whereas “$aParams” is an optional parameter. The return value “$oInstance” is an instance of the given class where everything is public. So you can access everything from outside.

Stop!!!
Do you want to post a comment like “bad coding style” or something similar? First you should be aware that this helper is intended to be only used for unit testing or other meta programming, but NEVER in productive code.

I myself think that’s fine ;) And of course it’s open source!

So please help Matze to win the “Innovation Award” by voting for him here.

© 2009 die musterdenker. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.