On 20/04/2023 08:11, Robert Landers wrote:
public function withStatus($code, $reasonPhrase = ''): Response
{
     return clone $this {
         $this->statusCode = $code;
          $this->reasonPhrase = "Old: $this->reasonPhrase, New: $reasonPhrase";
     }
};


Note that this is not the current proposed syntax. Since the keys are not dynamic, the current proposal is this:

public function withStatus($statusCode, $reasonPhrase = ''): Response {
  // perform validation here
  $reasonPhrase = "Old: $this->reasonPhrase, New: $reasonPhrase";
  return clone $this with {statusCode: $statusCode, reasonPhrase: 
$reasonPhrase};
}



public function withStatus($statusCode, $reasonPhrase = ''): Response {
   // perform validation here
   $reasonPhrase = "Old: $this->reasonPhrase, New: $reasonPhrase";
   return clone $this with compact('statusCode', 'reasonPhrase');
}


The compact() function always feels like a relic of the same era as create_function() and call_user_func(), both of which now have dedicated syntax.

That's what Nikita was talking about in the RFC section I quoted earlier: that compact('foo', 'bar') could be replaced with a dedicated syntax like [:$foo, :$bar]

So if we insisted on arrays, that would be:

public function withStatus($statusCode, $reasonPhrase = ''): Response {
  // perform validation here
  $reasonPhrase = "Old: $this->reasonPhrase, New: $reasonPhrase";
  return clone $this with [:$statusCode, :$reasonPhrase];
}

But I still don't see why an array should be the default case here, rather than using ... to unpack one if you really need to, like we do with arguments.

public function withStatus($statusCode, $reasonPhrase = null): Response {
  $newProps = [:$statusCode];
  if ( $reasonPhrase !== null ) {
      $newProps['reasonPhrase'] = "Old: $this->reasonPhrase, New: 
$reasonPhrase";
  }
  return clone $this with (...$newProps);
}


Regards,

--
Rowan Tommins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to