View Full Version : Handling $_GET and $_POST vars in OOP
reject
02-04-2008, 07:52 AM
Hi, which is the best way to handle $_GET and $_POST vars in OOP.
Say two examples:
1)
<?php
class myClass {
private $myVar;
public function __construct() {
$this->myVar = $_GET['myVar'];
$this->validateMyVar();
}
private function validateMyVar() {
// method body
}
}
$mc = new myClass();
?>
2)
<?php
class myClass {
private $myVar;
public function __construct($myVar) {
$this->myVar = $myVar;
$this->validateMyVar();
}
private function validateMyVar() {
// method body
}
}
$mc = new myClass($_GET['myVar']);
?>
oesxyl
02-04-2008, 08:28 AM
you intend to write a class for each specific variable you pass from GET and POST?
I doubt that you can reuse validateMyVar method somewhere.
Despite that, a big mistake in oop design is to make everything you have a class
avoiding systematic the use of function. Well this is more a guess, :)
best regards
reject
02-04-2008, 08:44 AM
Yeah your right, its not a very good example.
Lets say it is a pager class, where the page is passed in the url and accessed via the get method. The validating function could make sure wether the variable is an integer.
Does that make it any easier?
oesxyl
02-04-2008, 09:03 AM
Yeah your right, its not a very good example.
Lets say it is a pager class, where the page is passed in the url and accessed via the get method. The validating function could make sure wether the variable is an integer.
Does that make it any easier?
let's say a more complex case: you check if a variable is string and make sure is not '' and if is integer to make sure there is an integer, maybe positive integer and less then 10000.
Any combination you find and in such a general context make you to discover that is more easy to write a function and use it or iterate the post or get array then writing a class.
If you make the validation most specific you must write more function, but is more easy to write a function then a class.
Is a matter of design. If you need more function is possible to find some condition you can group into a function or a class, and reuse leter, but this is specific to a particular application.
best regards
reject
02-04-2008, 09:17 AM
Thanks, you've given me something to think about :D
firepages
02-04-2008, 02:02 PM
My 5c, I use the second method, for several reasons, the main one is the reason for using classes in the first place, portable reusable code.
e.g. , I have a set of classes that get used by both web-based aplications and GUI's (PHP-GTK)
In php-gtk $_POST and $_GET are not relevant (though you can use those superglobals anyway) and $_REQUEST data is either carried via $_SESSION or other object properties.
In your second example $myvar can come from anywhere, your class does not (nor should it) care. The obvious gotchya to this is input validation though this would ideally be handled long before your class gets hold of the data.
On the validation side I don't personally see why a function is any more useful than a class ??? indeed a validation class again need not care where the data comes from, it either passes or fails?
hammer65
02-04-2008, 03:36 PM
Given that one of the strengths of OOP is code reuse. Using a specific global name for any given piece of data, is contrary to that. It's no different than using the global keyword inside functions and classes.
Whatever you name a container of data, that name should be confined to a specific scope and not forced to be universal. If it is then you are tied to using specific names for various things in your code. ie. your database connection resource HAS to ALWAYS be called $db because all of your code expects it to, including your supposedly re-usable functions and classes.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.