Re: [PHP-DEV] Type casting syntax

This is only part of a thread. view whole thread
  115409
July 12, 2021 14:54 maxsem.wiki@gmail.com (Max Semenik)
I was planning to propose something closer to

On Mon, Jul 12, 2021 at 2:19 AM Larry Garfield <larry@garfieldtech.com>
wrote:

> What are the use cases for integrated object to object (ie, class to > class) conversion in PHP? Rust's type system is a very different beast, so > its casting logic makes more sense there. I'm not entirely clear on what > the use case is in PHP. When would that be superior to "just write an > asFoo() method and move on with life"? > > Scalar conversion I can see, but every time someone suggests adding > siblings to __toString(), there's major pushback. >
I was thinking of something akin to many compiled languages' approach of "consider this expression is now of that type, and throw an exception if it's not". An example of this approach from Benjamin's proposal of old^ $service = (EmailService) $diContainer->get('email.service'); Instead of /** @var EmailService $service */ $service = $diContainer->get('email.service'); if (!$service instanceof EmailService) { throw new TypeError('Expected instance of EmailService, ...'); } -- Best regards, Max Semenik