Req #80208 [NEW]: Scalar pseudo-type (like Iterable)
| From: | divinity76 at gmail dot com | Date: | Fri, 09 Oct 2020 11:15:24 +0000 |
| Subject: | Req #80208 [NEW]: Scalar pseudo-type (like Iterable) | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-229481@lists.php.net to get a copy of this message | ||
From: divinity76 at gmail dot com
Operating system:
PHP version: Next Minor Version
Package: Unknown/Other Function
Bug Type: Feature/Change Request
Bug description:Scalar pseudo-type (like Iterable)
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.
--
Edit bug report at https://bugs.php.net/bug.php?id=80208&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80208&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80208&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80208&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80208&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80208&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80208&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80208&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80208&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80208&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80208&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80208&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80208&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80208&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80208&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80208&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80208&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80208&r=mysqlcfg