PHP Coding Standards – Setting $_POST Variables vs Passing Parameters in Functions

coding-standardsfunctionsPHP

I've got a legacy PHP web application wherein almost each and every function makes references to $_POST variables – retrieving their values, AND setting them (or setting new POST variables) as a means to communicate with the calling code or other functions.

eg:

function addevent()
{
...
$add_minutes=$_POST['minutes'];     
...
$_POST['event_price']=''; 
...
}

I would have thought that the straightforward approach would have been to pass to a function all the values that it needs, and return all what they generate.

As an old-school programmer, albeit a bit out of touch now, I find this structure grotesquely unsettling – where data is passed arbitrarily all over the place. What do others think? Is the above approach acceptable now?


Edit 1

One conceivably genuine use-case is as follows. This is a function which handles embed tags or an uploaded file.

function brandEvent($edit_id='')
{
...
    switch($_POST['upload_or_embed'])
    {
        case 'embed':
        ...
        // uses $_POST['embed_video_code']
        ...
        break;
        case 'upload':
        ...
        // uses DIFFERENT POST variables
        ...
    }
}

Is this a sensible code structure?

Best Answer

As an old-school programmer, albeit a bit out of touch now, I find this structure grotesquely unsettling - where data is passed arbitrarily all over the place. What do others think?

I, too, find this structure grotesquely unsettling. Those functions should be atomic, independent of external influences. Your examples can be called programming by coincidence and this is a horrible practice. It's unrobust code depending on side-effects and globals and will likely cause headaches for maintenance programmers. This style also effectively limits the reusability and testability of those functions, as they are so tightly coupled to a specific POST of variables (probably from the view).

This approach is by no means acceptable.

Related Topic