php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80720 Class, Interface and Trait pseudo-types
Submitted: 2021-02-07 08:07 UTC Modified: 2021-02-07 08:09 UTC
From: klaussantana at gmail dot com Assigned:
Status: Suspended Package: Unknown/Other Function
PHP Version: 8.0.2 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
10 + 7 = ?
Subscribe to this entry?

 
 [2021-02-07 08:07 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-07 08:09 UTC] requinix@php.net
-Status: Open +Status: Suspended
 [2021-02-07 08:09 UTC] requinix@php.net
> This feature can be further discussed.
Indeed.

https://wiki.php.net/rfc/howto
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 12:01:31 2024 UTC