php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67097 Allow traits to require class/interface
Submitted: 2014-04-18 21:17 UTC Modified: 2017-09-10 21:10 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: thierry at byscripts dot info Assigned:
Status: Suspended Package: Class/Object related
PHP Version: 5.5.11 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-04-18 21:17 UTC] thierry at byscripts dot info
Description:
------------
I think we need to be able to allow traits to require that the class in which it can be used must be an instance of an arbitrary class/interface.

Expected result:
----------------
trait AirConditioning requires Vehicle
{
    public function increaseTemperature() { $this->ambientTemperature++; }
    public function decreaseTemperature() { $this->ambientTemperature--; }
}

abstract class Vehicle
{
    protected $ambientTemperature;
}

class Car extends Vehicle {}
class Truck extends Vehicle {}
class Dog extends Animal {}

class MyOldCar extends Car {}
class MyNewCar extends Car
{
    use AirConditioning; // Should be OK
}

class MyLabrador extends Dog
{
    use AirConditioning; // Should throw an exception
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-20 02:01 UTC] levim@php.net
My $0.02: allow traits to specify that they implement Interfaces. This is their #1 use already.
 [2017-09-10 21:10 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2017-09-10 21:10 UTC] cmb@php.net
Apparently, this proposal is controversial and as such would
require an RFC, see <https://wiki.php.net/rfc>. Everybody is
welcome to create a respective RFC; if you do so please refer to
this feature request. In the meantime, I'm suspending this ticket.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Oct 25 16:01:24 2020 UTC