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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: klaussantana at gmail dot com
New email:
PHP Version: OS:

 

 [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 May 23 02:01:30 2024 UTC