Simple Function Testing and Debugging in PHP

Published in Programming and Scripts on Friday, August 10th, 2007

When programming for the web, sometimes the need arises to test a function on the fly without being too intrusive. You may be debugging and need to test for a result, or simply testing. The following is a simple strategy that can help in those cases.

Firing a function from your browser

The concept is as simple as firing a function from your browser, and it leans on PHP's call_user_func_array.

I'm going to outline the concept as I have implemented it. This exact implementation may not work in your case, but perhaps you can adapt it to do so.

if(isset($_GET['f']) && function_exists($_GET['f'])) {
$func = $_GET['f']; // Get function name.
unset($_GET['f']); // Drop function from from get.
// Fire and print function, passing 
// remaining GETs as function parameters.	
print_r(call_user_func_array($func, $_GET)); 
exit;
}

In our CMS/Framework, we set up a controller with the code from above to respond at a given URL, for example http://www.example.com/__FOO. By passing a function name as a GET variable, in this case 'f', and the parameters necessary for that function to work as subsequent GET parameters, the result of that function will be printed to the screen.

So, http://www.example.com/__FOO?f=hello_foo&a=world would fire the function hello_foo('world'), perhaps printing Hello World! to the screen.

This allows for a quick and dirty test of a given function, and can be done remotely on a live site, if necessary, without touching any files or whatnot.

We hide this behind an authorization wall and also clean our parameters before they get to this level, so if you try this, keep these points in mind.

Comments and Feedback

This is an iteresting debuging. I have yet to look into debuging and error handeling in any organised way. Is your framework avalible to be looked at? I have not been impressed with the PHP frameworks that are distributed. Although I understand the task of catering for everyones wants is huge. The biggest gap that need to be filled is a framework for forms that allows advanced forms to be created in a short ammount of time, results lists with paging and result list fliters. Those are my main areas to explore in PHP as well as JavaScript style events.

Coincidently, I just wrote about this same function a few minutes ago: http://www.mutinydesign.co.uk/scripts/problems-encountered-with-php-dom-functions---3/

thats quick and dirty alright... just dont forget to remove it when the project is finished to avoid xss attacks etc :)

That\'s a quick and easy solution, thanks for sharing it!

Interesting PHP technique for testing functions. I\'m just wondering what happens when malicious users start typing in their own function names or paths outside your site.

@Daniel: Well you generally shouldn\'t do any debugging live, at least not available publicly.

I never knew about this though and with it being that easy theres no excuse not to really. Thanks

Home » Blog » Web Development » Programming and Scripts

Check out the blog categories for older content

The latest from my personal website,
Mike Papageorge.com

SiteUptime Web Site Monitoring Service

Sitepoint's web devlopment books have helped me out on many occasions both for finding a quick solution to a problem but also to level out my knowlegde in weaker areas (JavaScript, I'm looking at you!). I am recommending the following titles from my bookshelf:

The Principles Of Successful Freelancing

I started freelancing by diving in head first and getting on with it. Many years and a lot of experience later I was still able to take away some gems from this book, and there are plenty I wish I had thought of beforehand. If you are new to freelancing and have a lot of questions (or maybe don't know what questions to ask!) do yourself a favor and at least check out the sample chapters.

The Art & Science Of JavaScript

The author line-up for this book says it all. 7 excellent developers show you how to get your JavaScript coding up to speed with 7 chapters of great theory, code and examples. Metaprogramming with JavaScript (chapter 5 from Dan Webb) really helped me iron out some things I was missing about JavaScript. That said each chapter really helped me to develop my JavaScript skills beyond simple Ajax calls and html insertion with libs like JQuery.

The PHP Anthology: 101 Essential Tips, Tricks & Hacks

Like the other books listed here, this provides a great reference for the PHP developer looking to have the right answers from the right people at their fingertips. I tend to pull this off the shelf when I need to delve into new territory and usually find a workable solution to keep development moving. This only needs to happen once and you recoup the price of the book in time saved from having to develop the solution or find the right pattern for getting the job done..