Req #80720 [Opn->Sus]: Class, Interface and Trait pseudo-types
Edit report at https://bugs.php.net/bug.php?id=80720&edit=1
ID: 80720
Updated by: requinix@php.net
Reported by: klaussantana at gmail dot com
Summary: Class, Interface and Trait pseudo-types
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: Unknown/Other Function
Operating System: Any
PHP Version: 8.0.2
Block user comment: N
Private report: N
New Comment:
> This feature can be further discussed.
Indeed.
https://wiki.php.net/rfc/howto
Previous Comments:
------------------------------------------------------------------------
[2021-02-07 08:07:59] klaussantana at gmail dot com
Description:
------------
---
From manual page: https://php.net/language.types.declarations
---
If you use those pseudo-types to define a parameter|variable|UnionType, it'll assume a String
value containing a valid Class|Interface|Trait full qualified name only if it passes through their
respective *_exists functions. It can also assume an object as value.
Otherwise a runtime exception must be thrown because that no class, interface or trait were defined
with the String value as name, even after autoload performance.
This feature can be further discussed.
Test script:
---------------
<?PHP
fn ( Class|Interface|Trait|Object $Subject )
{
if ( is_string($Subject) )
echo 'The parameter $Subject is a valid Class|Interface|Trait name.';
elseif ( is_object($Subject) )
echo 'The parameter $Subject is an object of any class.';
elseif ( $Subject === null )
echo 'The parameter $Subject is empty.';
else
echo 'This code line will never run.';
echo PHP_EOL;
}
fn ( new StdClass ); // Works.
fn ( function () {} ); // Works.
fn ( 'Defined' ); // Works.
fn ( 'Undefined' ); // Throws some runtime exception because there's no class,
interface or trait defined with that name.
Expected result:
----------------
The parameter $Subject is an object of any class.
The parameter $Subject is an object of any class.
The parameter $Subject is a valid Class|Interface|Trait name.
RuntimeException: Invalid parameters content.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80720&edit=1
Thread (2 messages)