Question

Is it okay to wrap a global array in a class to hide the undefined errors (and returning false) with OOP? If not, why?

I'm using the following Input wrapper class. Mainly using it together with my FormValidator class so that I don't have to write isset() for N amount of inputs I want to use / check.

class Input {
    private $_source;

    public function __construct(Array $source){
        $this->_source = $source;
    }

    public function exists() {
        return !empty($this->_source);
    }

    public function get($input) {
        return (isset($this->_source[$input])) ? $this->_source[$input] : false;
    }
}
Was it helpful?

Solution

As good comments on your question points out, there is different ways and maybe better built-in concept you could use to solve the problem you pose.

But to answer your question regarding OOP practices in PHP, I would definitively argue that hiding errors in your wrapper class is legal.

I would regard it as a blackbox and the concept of that error might not be known outside since your interface does not document it.

An error handling could be implemented inside your wrapper to later handle those errors in a way that does not need to be known on the outside.

It ok, but all errors should be handled in general and hiding them or swallowing them could give strange behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top