Avoid passing variables to classes all the time

Buzzer

New Member
I'm creating a web app in PHP5. So far, I have two "global" variables: $input and $sql. $input is the validated and sanitized input from $_GET, $_POST and $_COOKIE. $sql is an instance of my mysql(i) class.These two are needed in almost every other class. This could be relatively easily achieved by passing them as parameters to the __construct function of each class. But that seems ... clumsy. Along with more class specific parameters and potential future global vars, it makes for unwieldy function calls.The easy and, as I understand it, noobie alternative would be using the global keyword. I don't want to do that. I get the downsides of globals, although in my scenario (small app) they wouldn't matter much. But it's also clumsy to explicitely label them global before using them again.A more elegant way I suppose: I have a webapp class from which I extend all other classes. This webapp class holds common functions I need in more than one other class but that do not warrant for a seperate class.If I store $input and $sql as static variables in this webapp master class, they'd be accessible in all subclasses.But I understand that static variables are as much frowned upon, if not more, than global variables.Is that true?I guess I'm one of these people who overthink everything, but I like elegant code and this looks elegant to my (amateur) eyes.
 
Back
Top