|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2020-10-09 11:26 UTC] divinity76 at gmail dot com
[2020-10-09 11:36 UTC] kocsismate@php.net
[2020-10-09 12:01 UTC] cmb@php.net
-Status: Open
+Status: Suspended
[2020-10-09 12:01 UTC] cmb@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 13:00:01 2025 UTC |
Description: ------------ i suggest a scalar pseudo-type, much like Iterable, today if one wants to accept scalar arguments, one has to write something like: <?php function f($sc){ if(!is_string($sc) && !is_int($sc) && !is_float($sc) && !is_bool($sc)){ throw new \InvalidArgumentException('argument #1 ($sc) must be a scalar type'); } } class C{ public $sc; function set_sc($sc){ if(!is_string($sc) && !is_int($sc) && !is_float($sc) && !is_bool($sc)){ throw new \InvalidArgumentException('argument #1 ($sc) must be a scalar type'); } $this->sc = $sc; } } ?> luckily it gets easier in PHP8, <?php function f(string|int|float|bool $sc){ } class C{ public string|int|float|bool $sc } ?> even so, if there was a Scalar pseudo-type, i would use it. Test script: --------------- <?php declare(strict_types=1); function fun(Scalar $sc){ var_dump($sc); } fun(1); Expected result: ---------------- int(1) Actual result: -------------- Fatal error: Uncaught TypeError: fun(): Argument #1 ($sc) must be of type Scalar, int given, called in /in/PDjNq on line 6 and defined in /in/PDjNq:3 Stack trace: #0 /in/PDjNq(6): fun(1) #1 {main} thrown in /in/PDjNq on line 3 Process exited with code 255.