php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80208 Scalar pseudo-type (like Iterable)
Submitted: 2020-10-09 11:15 UTC Modified: 2020-10-09 12:01 UTC
From: divinity76 at gmail dot com Assigned:
Status: Suspended Package: Unknown/Other Function
PHP Version: Next Minor Version OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-10-09 11:15 UTC] divinity76 at gmail dot com
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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-09 11:26 UTC] divinity76 at gmail dot com
... i should have googled more, apparently it's already an "RFC under discussion", https://wiki.php.net/rfc/scalar-pseudo-type
 [2020-10-09 11:36 UTC] kocsismate@php.net
I have a fairly up-to-date PR exactly for this which I don't plan to pursue:  https://github.com/php/php-src/pull/5483. Please feel free to start an RFC by using this implementation if you would like this functionality in PHP.
 [2020-10-09 12:01 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-10-09 12:01 UTC] cmb@php.net
Yes, that would need an RFC.  Feel free to pursue.  For the time
being, I'm suspending this ticket.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 00:01:28 2024 UTC