php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67310 Allow abstract private functions in traits
Submitted: 2014-05-20 11:41 UTC Modified: 2020-02-07 09:59 UTC
Votes:7
Avg. Score:3.6 ± 0.5
Reproduced:7 of 7 (100.0%)
Same Version:2 (28.6%)
Same OS:4 (57.1%)
From: chrisb at zedcore dot com Assigned: nikic (profile)
Status: Assigned Package: Scripting Engine problem
PHP Version: 5.4.28 OS: Debian GNU/Linux "wheezy"
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: chrisb at zedcore dot com
New email:
PHP Version: OS:

 

 [2014-05-20 11:41 UTC] chrisb at zedcore dot com
Description:
------------
When using traits, the trait and the class using the trait can both access private members of the class. The trait can also define abstract methods to enforce that the class using the trait implements them.

However, it's not (currently) possible to define an abstract private method; it throws a fatal error. I assume this check was add pre-traits, where it obviously didn't make sense to have abstract private methods since they could never be overridden, but traits change this – an abstract private method in a trait *can* be overridden by the class using the trait. Therefore I think that it would make sense for the this check to be changed so that traits can define methods as "abstract private".

The test script attached causes a fatal error. Replacing the trait's "MyPrivateMethod" with an empty non-abstract function makes it print out "Hello World" as expected.

Test script:
---------------
trait MyTrait
{
        abstract private function MyPrivateMethod();
        public function MyPublicMethod() {
                $this->MyMethod();
        }
}

class MyClass {
        use MyTrait;
        private function MyPrivateMethod() {
                echo "Hello World";
        }
}

$o = new MyClass();
$o->MyPublicMethod();


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-05-22 22:42 UTC] aharvey@php.net
Implemented as https://github.com/php/php-src/pull/682, but we'll need someone with Zend karma to merge it.
 [2014-06-06 12:53 UTC] kjarli at gmail dot com
I could be wrong.. But why would a trait have to tell a class what to implement? That's the job of interfaces. Traits are just to add generic functionality to support "multiple inheritance". Therefore I highly disagree with traits being able to mark _anything_ as abstract and should abstract traits not be possible in my opinion.
 [2014-06-09 16:31 UTC] aharvey@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: aharvey
 [2014-06-09 16:31 UTC] aharvey@php.net
I'm going to RFC this post-5.6.0.
 [2014-06-12 14:33 UTC] chrisb at zedcore dot com
Abstract methods in traits are useful in the case where you have generic functionality which needs a certain amount of information from the class it is being used by. Being able to add abstract methods to traits means you can define accessor methods to get that data. The "abstract trait members" example *in the PHP manual* is a perfect example of this.

It's not currently possible for a trait to enforce that the using class implements a particular interface, so I don't see how that's relevant.
 [2017-10-24 07:18 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: aharvey +Assigned To:
 [2020-02-07 09:59 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Mar 28 18:01:24 2020 UTC