PHP Response Wrapper I'm writing a routing system that may or may not be part of a public API later for a personal project. A main part of the routing system is a response object for the user to send headers, status code, and the body of the response that is sent from the server. 

I've created a response class that I think covers everything the user would want to do with the response (it could use a few more "convenience" functions however). I wanted to post it here to make sure my code is clean, and that I didn't miss anything that the end user might want to do.

An instance of the request class is provided to the user when a request is made to a specified URL (see my last code review) 

I present my response class: 

    class Response {

    private $headers = array();
    private $code = 200;
    private $body = '';
    private $sent = false;

    private $log;

    public function __construct() {
        $this-&gt;log = Logger::getLogger(get_class($this));
    }

    public function headers ($headers) { // Add some headers to the headers array
        $this-&gt;headers = array_merge($this-&gt;headers, $headers);
        return $this;
    }

    public function header ($key, $value) { // set a single header in the headers array
        $this-&gt;headers[$key] = $value;
        return $this;
    }

    public function code ($code) { // Set the status code
        $this-&gt;code = $code;
        return $this;
    }

    public function status ($code) { // Alternate method for setting the status code
        return $this-&gt;code($code);;
    }

    public function json($str) { // respond with json, set the body text and set the content-type header
        $this-&gt;header('Content-Type', 'application/json');
        if(is_array($str)) { // handle either raw JSON text, or php arrays
            $this-&gt;body = json_encode($str);
        } else {
            $this-&gt;body = $str;
        }
        return $this;
    }

    public function html ($str){ // respond with HTML
        $this-&gt;header('Content-Type', 'text/html');
        $this-&gt;body = $str;
        return $this;
    }

    public function form ($str) { // Respond with form data
        $this-&gt;header('Content-Type', 'application/x-www-form-urlencoded');
        // TODO: Allow the user to user an array
        $this-&gt;body = $str;
        return $this;
    }

    public function render ($file){ // Render an HTML file from the templates folder
        //TODO: Restrict to templates folder open
        //TODO: Add server-side rendering code
        $this-&gt;body = file_get_contents($file);
        return $this;
    }

    public function sent() { // Check if the request has been sent
        return $this-&gt;sent;
    }

    public function send () { // send the request
        if($this-&gt;sent()){
            $log-&gt;error('Attempted to call send on a sent response!');
            exit('Attempted to call send on a sent response!');
        }
        // Log the request for debugging
        $this-&gt;log-&gt;info($this-&gt;headers);
        $this-&gt;log-&gt;info('HTTP Responce Code: ' . $this-&gt;code);
        $this-&gt;log-&gt;info($this-&gt;body);
        // Set the headers
        foreach($this-&gt;headers as $key =&gt; $value) {
            header($key .': ' . $value);
        }
        // Set the status code
        http_response_code($this-&gt;code);

        // Send out the body
        echo $this-&gt;body;

        // Set the sent variable
        $this-&gt;sent = true;
    }
}


EDIT:
The idea with the short and clear function names that all return $this is to be able to have clear and simple chainable responses. (e.g $res-&gt;code(200)-&gt;json(... Json...);)
I this this class is not a Response. But a bundle of things.

class Response
{


Name its clear. But if it is clear, I suggest you to make this class final.

    private $headers = array();
    private $code = 200;
    private $body = '';
    private $sent = false;


By default a non sent response is 200 and without headers?

    private $log;


Here my first big confusion. Why a response should log something? A Response is just a response.

    public function __construct() {
        $this-&gt;log = Logger::getLogger(get_class($this));
    }


Alt ^_^. I really suggest you to inject a logger. If you like standards, I suggest to inject Psr\Log\LoggerInterface

    public function headers ($headers) { // Add some headers to the headers array
        $this-&gt;headers = array_merge($this-&gt;headers, $headers);
        return $this;
    }


I suggest 'set' prefix. From the api I do not understand if the method is a setter or a getter.

    public function header ($key, $value) { // set a single header in the headers array
        $this-&gt;headers[$key] = $value;
        return $this;
    }


As above: if this is a setter, ... I suggest 'set' prefix.

    public function code ($code) { // Set the status code
        $this-&gt;code = $code;
        return $this;
    }


Instead of comment "set the status code" change the method in `public function setStatusCode()".

    public function status ($code) { // Alternate method for setting the status code
        return $this-&gt;code($code);;
    }


Why?

    public function json($str) { // respond with json, set the body text and set the content-type header
        $this-&gt;header('Content-Type', 'application/json');
        if(is_array($str)) { // handle either raw JSON text, or php arrays


Why dont you use type hinting and create a method that accept array and one that accept text?

            $this-&gt;body = json_encode($str);
        } else {
            $this-&gt;body = $str;
        }
        return $this;
    }

    public function html ($str){ // respond with HTML


A method called toHtml is more appropriate here.

        $this-&gt;header('Content-Type', 'text/html');
        $this-&gt;body = $str;
        return $this;


Fluent interface is ok just for builders. This class should be rename in ResponseBuilder.

    }

    public function form ($str) { // Respond with form data


Why you comment your code instead of make it more readable?

        $this-&gt;header('Content-Type', 'application/x-www-form-urlencoded');
        // TODO: Allow the user to user an array
        $this-&gt;body = $str;
        return $this;
    }

    public function render ($file){ // Render an HTML file from the templates folder
        //TODO: Restrict to templates folder open
        //TODO: Add server-side rendering code
        $this-&gt;body = file_get_contents($file);
        return $this;
    }

    public function sent() { // Check if the request has been sent
        return $this-&gt;sent;
    }


This method return a boolan. Should be named to isSent. And also should have a return type hint like public function isSent() : bool.

    public function send () { // send the request


Comment is useless. $request-&gt;send() speaks very well.

        if($this-&gt;sent()){
            $log-&gt;error('Attempted to call send on a sent response!');
            exit('Attempted to call send on a sent response!');


I prefer Exceptions every time something unexpected happens.

        }

        // Log the request for debugging
        $this-&gt;log-&gt;info($this-&gt;headers);
        $this-&gt;log-&gt;info('HTTP Responce Code: ' . $this-&gt;code);
        $this-&gt;log-&gt;info($this-&gt;body);


Anytime you use a comment to group some lines of code, you should remove the comment and make a private method like $this-&gt;logRequestForDebugging().

        // Set the headers
        foreach($this-&gt;headers as $key =&gt; $value) {
            header($key .': ' . $value);
        }


$this-&gt;setHeaders()

        // Set the status code
        http_response_code($this-&gt;code);


$this-&gt;setResponseStatusCode()

        // Send out the body
        echo $this-&gt;body;


...

        // Set the sent variable
        $this-&gt;sent = true;
    }
}