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.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BlinkList
  • LinkedIn
  • MisterWong
  • MySpace
  • Netvibes
  • NewsVine
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Yigg

Tags: , , ,

3 Responses to “Changing scope in PHP for unit testing”


  1. Sebastian Bergmann
    on Okt 15th, 2009
    @ 08:39

    Starting with PHP 5.3.2 you will not need this “Super Proxy” as the Reflection API allows you to invoke private and protected methods.


  2. mathias
    on Okt 16th, 2009
    @ 11:10

    I know, but it will take some time till all projects use PHP > 5.3.2 … I fear it will take around 30 years ;)


  3. Jürgen Gramenz
    on Nov 19th, 2009
    @ 10:52

    I use Matze’s SuperProxy for unit testing of Zonk! Light Framework for PHP. The SuperProxy is a perfect tool to avoid breaking class architecture only to make private methods testable. (And YES: i think You should test private methods too!) Of course with PHP 5.3.2 You shouldn’t have to use this SuperProxy, but I agree with Mathias….it will take a lot of time to production state of this version.

Leave a Reply

© 2009 die musterdenker. All Rights Reserved.

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