views:

57

answers:

2

Hi

was thinking about creating a Single class called 'Request' to handle and clean POST and GET variables but being new to the singleton pattern I'm not sure how to implement it. Ideally I'd like to have 2 functions post($name,$clean) and get($name,$clean) - $clean being a boolean to determine whether to trim/ escape the value

A: 

I'm not sure why you think this is a good candidate for a singleton - I've have thought a simple static class would make much more sense. (Incidentally, I presume you're using the built in filter functions?)

Whilst a gross simplification, singleton's are good at limiting/controlling access to a finite resource which isn't really the case in this instance.

middaparka
A static class isnt too smart either. You'd still have a hard time mocking it. IMO, the reason to use a Request object is to decouple the actual Request from the environment, including the global scope.
Gordon
To be honest it doesn't have to be singleton - im still trying to understand what situations are more suited to singleton actually.In terms of sanitizing i was actually only thinking about trim and strip_tags I didnt even know about these filter functions - they look promising!
@Gordon - exactly, I want to decouple from the environment/global scope i.e. a standard object whereby i could access the vars knowing they had been sanitized without having to repeatedly use trim/strip_tags which is what im current doing on each form or query string varHow would you create this Class?
@Gordon - True in terms of mocking - I was just thinking of ease of use.
middaparka
@mindfriction check out the links in [my answer about input handling](http://stackoverflow.com/questions/2532849/handling-input-with-zend-framework-outside-mvc/2534538#2534538). They point to example implementations you might find useful.
Gordon
Thanks heaps Gordon!
@Gordon: So long as a static class does not have it's member variables private, a subclass can override the visibility of the variables. So you can have a static Request class, and in a test case declare a TestingRequest class that exposes the static variables in the Request class. However, this requires more planning in advance than a Singleton, which needs all data in member variables. Private is bad there too.
jmz
@jmz Im not saying its impossible. im just saying it sucks and doesnt pay. Static methods and classes are harder to test and you usually gain nothing from having them static. See http://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html
Gordon
A: 

I think using a singleton would be great even if for practice. Just because someone doesn't think it's the right approach does not make it wrong. By using and implementing ideas, not matter how small or large, you will learn and expand your knowledge of said implementations. You will soon know if it works for your particular situation or not. Then you do not have to take the opinions of others.

That being said, I say go for it. I can see why you would want to. The thought being, "I want everything to be cleaned for injections (etc.) and this way I can make sure it will happen on every input."

Allow me to shed a little light. Why not implement a single method using $_REQUEST that will process both $_GET and $_POST? While this may be a simple approach, it will get you started.

$cleanser = Cleanser::singleton();
$new_request_array = $cleanser->clean($_REQUEST);

class Cleanser
{
    private static $instance;
    private function __construct() { }

    public static function singleton() {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }

    public function clean($request) {
        foreach($request as $key => $value) {
            // perform any cleansing here
            $cleansed[$key] = trim($value);
        }

        return $cleansed;
    }

    public function __clone() {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
}

Have fun and remember, there is nothing "wrong" or "incorrect" when it comes to learning. I think that is the approach StackOverflow is trying to make. We are hear to learn how to do something, not be judged on our implementation of it. So have fun!

cdburgess
@cdburgess - Good point its the only way im really going to get an understanding with good ol' trial and error, thanks!