Re: Object Type Casting Reloaded
| From: | Mark Randall | Date: | Tue, 23 Apr 2019 13:57:45 +0000 |
| Subject: | Re: Object Type Casting Reloaded | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-105360@lists.php.net to get a copy of this message | ||
On 23/04/2019 14:39, Mark Randall wrote:
It keeps coming back to the feature we've all been praying to the PHP Gods for... generics.Replying to myself (sorry). I just wanted to point out that the same syntax would also solve your other issue from the nullable casts thread: function ncast<type T>($value) ?T { if ($value === null) {
return null;} if (settype($value, T) === false) {
throw new InvalidArgumentException('Cannot convert to ' . T);
}
return $value;
}
ncast<int>(null);
ncast<int>(12345);
ncast<string>(null);
ncast<string>('abcdef');
--
Mark Randall