Using globals makes your code hard to test thus more prone to bugs, not secure and unpredictable. That's why we pass the variables we want inside a function/object. So my question is simple:
Do $_POST, $_GET, etc violate the encapsulation principle?
I'm thinking that, to retain control of those variables in an OO way, an ideal solution would be to add some lines like this to the code:
// Convert the $_GET array to an object $get = json_decode(json_encode($_GET), FALSE); // stackoverflow.com/a/1869147 // Stop it from being included from anywhere unset($_GET); // Small example of what could be done later on $DB = new PDO(/* ... */); $Person = new Person($DB, $get->id);
I haven't seen this anywhere, not even a tutorial nor recommendation. Also, we can clearly see how the code above is much easier to be tested than one that includes
$Person = new Person($DB, $_GET['id']); or even (the ugly)
$Person = new Person($DB); as you can use a mock
Is the code above in the right direction or am I missing something?
EDIT: After some investigation (Zend framework and Cake PHP) as Alexander Kuzmin suggested, it seems to be the right thing to go. They're probably too big for me to dig into the code ATM, but I'll keep it in mind.