So, I don’t come from a huge PHP background—and I was wondering if in well formed code, one should use the ‘superglobals’ directly, e.g. in the middle of some function say $_SESSION['x'] = 'y'; or if, like I’d normally do with variables, it’s better to send them as arguments that can be used from there, e.g:
class Doer {
private $sess;
public function __construct(&$sess) {
$this->sess =& $sess;
}
}
$doer = new Doer($_SESSION);
and then use the Doer->sess version from within Doer and such. (The advantage of this method is that it makes clear that Doer uses $_SESSION.)
What’s the accepted PHP design approach for this problem?
I do like to wrap
$_SESSION,$_POST,$_GET, and$_COOKIEinto OOP structures.I use this method to centralize code that handles sanitation and validation, all of the necessary
isset ()checks, nonces,setcookieparameters, etc. It also allows client code to be more readable (and gives me the illusion that it’s more maintainable).It may be difficult to enforce use of this kind of structure, especially if there are multiple coders. With
$_GET,$_POST, and$_COOKIE(I believe), your initialization code can copy the data, then destroy the superglobal. Maybe a clever destructor could make this possible with $_SESSION (wipe $_SESSION on load, write it back in the destructor), though I haven’t tried.I don’t usually use any of these enforcement techniques, though. After getting used to it, seeing
$_SESSIONin code outside the session class just looks strange, and I mostly work solo.EDIT
Here’s some sample client code, in case it helps somebody. I’m sure looking at any of the major frameworks would give you better ideas…
Post and its friends all derive from a base class that implements the core validation code, adding their own specific functionality like form tokens, session cookie configuration, whatever.
Internally, the class holds a collection of valid data that’s extracted from
$_POSTas the validation methods are called, then returns them as properties using a magic__getmethod. Failed fields can’t be accessed this way. My validation methods (exceptrequired) don’t fail on empty fields, and many of them usefunc_get_argsto allow them to operate on multiple fields at once. Some of the methods (likemoney) automatically translate the data into custom value types.In the error case, I have a way to transform the data into a format that can be saved in the session and used to pre-populate the form and highlight errors after redirecting to the original form.
One way to improve on this would be to store the validation info in a Form class that’s used to render the form and power client-side validation, as well as cleaning the data after submission.