Why the PSR-7 specification uses WITHX instead of SetX in Requestinterface

Source: Internet
Author: User
Like in slimphp. RequestRealize.

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

With the implementation of all involved clone $object , clone will apply for a new variable space. Why not set it? A request to come over an Request object on it.

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

Reply content:

such as the implementation in slimphp Request .

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

With the implementation of all involved clone $object , clone will apply for a new variable space. Why not set it? A request to come over an Request object on it.

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

Good question!

Because the PSR-7 HTTP message and URI are both value objects ( value object ), one characteristic of the value object is that its value determines its uniqueness, that is, if all the values of the two value objects are the same, then they should be the same object; If two value objects have at least one value, they should be counted as two different objects. For this reason, when you are following a PSR-7 and you want to change Request a property, you should create another object instead of modifying it on the original object, so instead of clone simply assigning a value. Therefore, the value object is an immutable ( immutable ) object.

So, PSR-7 why use immutable value objects to standardize HTTP message? There are several reasons for this:

    1. Because the HTTP message itself is a immutable state. When a user sends a request to your program, the content of the request is fixed and no longer changes, and only the next same or different request is made.

    2. The value object can hold all the state of the original request, and any other program can get the original state.

The official fig provides a code for the benefit of a model value object:

$uri = new Uri('http://api.example.com');$baseRequest = new Request($uri, null, [    'Authorization' => 'Bearer ' . $token,    'Accept'        => 'application/json',]);;//上面构造基础 Request$request = $baseRequest->withUri($uri->withPath('/user'))->withMethod('GET');$response = $client->send($request);//基于上述基础Request构造一个GET请求,获得user的ID$body = new StringStream(json_encode(['tasks' => [    'Code',    'Coffee',]]));;$request = $baseRequest    ->withUri($uri->withPath('/tasks/user/' . $userId))    ->withMethod('POST')    ->withHeader('Content-Type', 'application/json')    ->withBody($body);$response = $client->send($request)//基于基础Request构造另一个POST请求,用刚才拿到的user ID添加task//如果不是使用值对象模型,我们将要重新从头开始构建这个Request$request = $baseRequest->withUri($uri->withPath('/tasks'))->withMethod('GET');$response = $client->send($request);//依然是基于基础Request构建其他请求

In fact, these are all explained in PSR-7 's meta document.
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.