2

After testing the code below produces the same results. My question is, is there a difference between the two at all?

 public function someaction1(SS_HTTPRequest $request) {
        $this->setResponse(new SS_HTTPResponse());
        $this->getResponse()->setStatusCode(400);
        $this->getResponse()->setBody('invalid');

        return $this->getResponse();
    }

    public function someaction2(SS_HTTPRequest $request) {
        $this->response = new SS_HTTPResponse();
        $this->response->setStatusCode(400);
        $this->response->setBody('invalid');

        return $this->response;
    }

To add, is return $this->response; or return $this->getResponse(); necessary or are they implicit?

  • 2
    getX() and setX() are [Mutator methods](https://en.wikipedia.org/wiki/Mutator_method), read [why to use them](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) – Greg Smirnov Mar 25 '16 at 12:55

1 Answers1

1

There is no difference, just open the parent class definition and see what getResponse() does:

public function getResponse() {
    return $this->response;
}

When you want to return HTTP error, it is better to use

$this->httpError(400, 'invalid request');

(no need to return, since it throws exception)

Greg Smirnov
  • 1,592
  • 9
  • 9