Referencing superglobals

I am working on a class to handle forms and I have a dilema as to which way to store the from data within the object.

It strikes me that storing the GET/POST superglobals as a property of the object might just be doubling up on resources ( still I would like a central location where to refer to the data fields of the form).

My other option , I think, is to store the superglobals as reference eg.:
$this->formDATA=($this->method=='GET') ? &$_GET : &$_POST;

What I would like to know is would doing this actually save any system memory? Is the new variable still a copy of the old that simply “checks back” or a “link” to the source?

Since the first thing your code should do is validate the entries passed in the super globals and to save the valid values into separate variables (where you can know throughout the subsequent processing that they must be valid) the super globals should not be getting anywhere near an object.

1 Like

My understanding is no. As long as you only ever read from the array, then PHP is smart enough to use references automatically under the hood.

As an aside, consider passing the form data in as an argument.


This way, your form code is less reliant on globals, and it’s also easier to test – you could pass in arbitrary values to see how it behaves.

$form->handleRequest(['field_name' => 'this is a test']);
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.