2016-03-25 16 views
2

以下のコードをテストした結果、同じ結果が得られます。私の質問は、両者の違いは何ですか?Silverstripeで作成されたレスポンスの差

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; 
    } 

追加するには、$ this-> responseが返されます。または$ this-> getResponse()を返します。必要なのか暗黙的なのか?違いはありません

+2

getX()およびsetX()は[Mutator methods](https://en.wikipedia.org/wiki/Mutator_method)です。 http://stackoverflow.com/questions/1568091/wh y-use-getters-and-setters) –

答えて

1

、単に親クラス定義を開いててgetResponse()は何を参照してください。

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

あなたはHTTPエラーを返すようにしたい、使用することをお勧めし

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

(例外をスローするので、返す必要はありません)

関連する問題