php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66927 Request new type operator "exhibits"
Submitted: 2014-03-18 17:29 UTC Modified: 2016-08-06 03:06 UTC
From: rdanner at gmail dot com Assigned: kalle (profile)
Status: Closed Package: *General Issues
PHP Version: 5.4.26 OS: Ubuntu 12.04 LTS
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rdanner at gmail dot com
New email:
PHP Version: OS:

 

 [2014-03-18 17:29 UTC] rdanner at gmail dot com
Description:
------------
Much like the "instanceof" operator can be used to evaluate if a class is and instance of (or descendant of) a particular class, an "exhibits" keyword would be useful for evaluating if a class exhibits a particular trait: if ($baz exhibits someTrait) {}



Test script:
---------------
//Currently, this can be done with something along the lines of

$ob = new someClass;

// see if $ob is an instance of a class that exhibits the "neato" trait
if (class_exists('someClass') and in_array('neato', class_uses($ob)) {
  // it does!
}

//but this would be much cleaner as:

if ($ob exhibits neato) {
  // yup!
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-08 10:56 UTC] minecraftshamrock at gmail dot com
I agree that such an operator would improve readability. But what would you do with such an operator? Knowing that a class uses a specific trait is nice, but the class might have changed the name or visibility of any of the taits' members.
 [2016-08-06 03:06 UTC] kalle@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: kalle
 [2016-08-06 03:06 UTC] kalle@php.net
Or maybe we could instead just modify the instanceof operator to support traits too, as classes/interfaces/traits all internally uses the same symbol table for naming, meaning this is disallowed:

trait A {}
interface A {} // Error, name is already in use

I suggest bringing this up at the mailing list (internals@) and perhaps consider writing an RFC for this that can target 7.2, as its too late for 7.1
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 14:01:31 2025 UTC