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 requestRequest
Object.
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 requestRequest
Object.
public function setAttribute($name, $value){ $this->attributes->set($name, $value); return $this;}
Good question!
BecausePSR-7
Indicates 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 changeRequest
When creating an attribute, you should create another object instead of modifying the original object.clone
Rather than simply assigning values. Therefore, a value object is an immutable (immutable
.
So,PSR-7
Why use an unchangeable value object to regulate HTTP Message? The reasons are as follows:
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.
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/