note 126191 added to language.types.declarations
| From: | toinenkayt at main dot php dot net | Date: | Thu, 24 Jun 2021 11:10:04 +0000 |
| Subject: | note 126191 added to language.types.declarations | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216543@lists.php.net to get a copy of this message | ||
While waiting for native support for typed arrays, here are a couple of alternative ways to ensure
strong typing of arrays by abusing variadic functions. The performance of these methods is a mystery
to the writer and so the responsibility of benchmarking them falls unto the reader.
PHP 5.6 added the splat operator (...) which is used to unpack arrays to be used as function
arguments. PHP 7.0 added scalar type hints. Latter versions of PHP have further improved the type
system. With these additions and improvements, it is possible to have a decent support for typed
arrays.
<?php
declare (strict_types=1);
function typeArrayNullInt(?int ...$arg): void {
}
function doSomething(array $ints): void {
(function (?int ...$arg) {})(...$ints);
// Alternatively,
(fn (?int ...$arg) => $arg)(...$ints);
// Or to avoid cluttering memory with too many closures
typeArrayNullInt(...$ints);
/* ... */
}
function doSomethingElse(?int ...$ints): void {
/* ... */
}
$ints = [1,2,3,4,null];
doSomething ($ints);
doSomethingElse (...$ints);
?>
Both methods work with all type declarations. The key idea here is to have the functions throw a
runtime error if they encounter a typing violation. The typing method used in doSomethingElse is
cleaner of the two but it disallows having any other parameters after the variadic parameter. It
also requires the call site to be aware of this typing implementation and unpack the array. The
method used in doSomething is messier but it does not require the call site to be aware of the
typing method as the unpacking is performed within the function. It is also less ambiguous as the
doSomethingElse would also accept n individual parameters where as doSomething only accepts an
array. doSomething's method is also easier to strip away if native typed array support is ever
added to PHP. Both of these methods only work for input parameters. An array return value type check
would need to take place at the call site.
If strict_types is not enabled, it may be desirable to return the coerced scalar values from the
type check function (e.g. floats and strings become integers) to ensure proper typing.
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 192.25.34.194)
----
Manual Page -- https://php.net/manual/en/language.types.declarations.php
Edit -- https://main.php.net/note/edit/126191
Del: integrated -- https://main.php.net/note/delete/126191/integrated
Del: useless -- https://main.php.net/note/delete/126191/useless
Del: bad code -- https://main.php.net/note/delete/126191/bad+code
Del: spam -- https://main.php.net/note/delete/126191/spam
Del: non-english -- https://main.php.net/note/delete/126191/non-english
Del: in docs -- https://main.php.net/note/delete/126191/in+docs
Del: other reasons-- https://main.php.net/note/delete/126191
Reject -- https://main.php.net/note/reject/126191
Search -- https://main.php.net/manage/user-notes.php