Why withX instead of setX is used in RequestInterface in the PSR-7 Specification

Source: Internet
Author: User
For example, the Request implementation in SlimPHP. {Code...} with involves clone $ object, and a new variable space will be applied for clone. Why not use set? You can send a Request object to a Request. {Code...}, for example, in SlimPHP Request.

public function withAttribute($name, $value){    $clone = clone $this;    $clone->attributes->set($name, $value);    return $clone;}

With implementation involvesclone $object, Clone will apply for a new variable space. Why not use set? One requestRequestObject.

public function setAttribute($name, $value){    $this->attributes->set($name, $value);    return $this;}

Reply content:

For example, in SlimPHPRequest.

public function withAttribute($name, $value){    $clone = clone $this;    $clone->attributes->set($name, $value);    return $clone;}

With implementation involvesclone $object, Clone will apply for a new variable space. Why not use set? One requestRequestObject.

public function setAttribute($name, $value){    $this->attributes->set($name, $value);    return $this;}

Good question!

BecausePSR-7Indicates that both HTTP Message and URI are value objects (value object), One feature of a value object is that its value determines its uniqueness. That is to say, if all values of the two value objects are the same, they should also be the same object. Conversely, if two value objects have at least one different value, they should be considered two different objects. For this reason, when you follow PSR-7 and you want to changeRequestWhen creating an attribute, you should create another object instead of modifying the original object.cloneRather than simply assigning values. Therefore, a value object is an immutable (immutable.

So,PSR-7Why use an unchangeable value object to regulate HTTP Message? The reasons are as follows:

  1. Because the HTTP Message itself is in an unchangeable state. When a user sends a request to your program, the content of the request after sending is fixed and will not be changed. There will only be one identical or different request.

  2. A value object can save all the states of the original request, and any other program can obtain this original state.

Official FIG provides a code to demonstrate the benefits of value objects:

$ Uri = new Uri ('HTTP: // api.example.com '); $ baseRequest = new Request ($ uri, null, ['authorization' => 'bearer '. $ token, 'access' => 'application/json',]); // construct the basic Request $ request = $ baseRequest-> withUri ($ uri-> withPath ('/user')-> withMethod ('get '); $ response = $ client-> send ($ request); // construct a GET Request based on the preceding basic request, obtain the user ID $ body = new StringStream (json_encode (['task' => ['code', 'coffee ',]); $ request = $ baseRequest-> withUri ($ uri-> withPath ('/tasks/user /'. $ userId)-> withMethod ('post')-> withHeader ('content-type', 'application/json')-> withBody ($ body ); $ response = $ client-> send ($ request) // construct another POST Request based on the basic request, and add the task with the user ID you just obtained. // if the value object model is not used, we will re-construct the Request $ request = $ baseRequest-> withUri ($ uri-> withPath ('/task')-> withMethod ('get '); $ response = $ client-> send ($ request); // other requests are still built based on the basic Request

In fact, these are described in the Meta Document of the PSR-7
Http://www.php-fig.org/psr/psr-7/meta/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.