0

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

hakre
  • 193,403
  • 52
  • 435
  • 836
experimenter
  • 768
  • 1
  • 9
  • 30
  • 2
    Why does Request have to be a Singleton? – Gordon Aug 13 '10 at 12:51
  • possible duplicate of [Creating the Singleton design pattern in PHP5](http://stackoverflow.com/questions/203336/creating-the-singleton-design-pattern-in-php5) – Gordon Aug 13 '10 at 12:53
  • 1
    While you're new to the Singleton pattern, you may want to read this article: http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/ – Ionuț G. Stan Aug 13 '10 at 12:58

2 Answers2

1

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 here to learn how to do something, not be judged on our implementation of it. So have fun!

Chuck Burgess
  • 11,600
  • 5
  • 41
  • 74
0

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.

John Parker
  • 54,048
  • 11
  • 129
  • 129
  • 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 Aug 13 '10 at 13:03
  • 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! – experimenter Aug 13 '10 at 13:10
  • @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 var How would you create this Class? – experimenter Aug 13 '10 at 13:13
  • @Gordon - True in terms of mocking - I was just thinking of ease of use. – John Parker Aug 13 '10 at 13:16
  • @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 Aug 13 '10 at 13:31
  • @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 Aug 14 '10 at 10:30
  • @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 Aug 14 '10 at 10:51