Req #67310 [Opn->Asn]: Allow abstract private functions in traits

From: Date: Mon, 09 Jun 2014 16:31:31 +0000
Subject: Req #67310 [Opn->Asn]: Allow abstract private functions in traits
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-186118@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67310&edit=1

 ID:                 67310
 Updated by:         aharvey@php.net
 Reported by:        chrisb at zedcore dot com
 Summary:            Allow abstract private functions in traits
-Status:             Open
+Status:             Assigned
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   Debian GNU/Linux "wheezy"
 PHP Version:        5.4.28
-Assigned To:        
+Assigned To:        aharvey
 Block user comment: N
 Private report:     N

 New Comment:

I'm going to RFC this post-5.6.0.


Previous Comments:
------------------------------------------------------------------------
[2014-06-06 12:53:37] 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-05-22 22:42:37] 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-05-20 11:41:04] 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();



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=67310&edit=1


Thread (7 messages)

« previous php.bugs (#186118) next »