Re: Traits with interfaces

From: Date: Fri, 02 Mar 2018 01:38:37 +0000
Subject: Re: Traits with interfaces
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-101952@lists.php.net to get a copy of this message
On Thu, Mar 1, 2018 at 11:04 PM, Rowan Collins <rowan.collins@gmail.com> wrote: > > What *can* break an interface's contract is changing the *visibility* of > the pasted method using "as protected" or "as private". This would need to > be captured somehow while composing the class, probably producing a > compile-time error, just as an explicit "implements" declaration would. To add to that, the receiving class can simply replace the trait method. So picking up from the same example: <?php interface Bobbable { public function bob(); } trait Bobber { public function bob() { echo "Bobbity"; } } class Bibble { use Bobber { bob as bib; } public function bob($arg) { return $arg; } } $b = new Bibble; $b->bob(); $b->bib(); Bibble is no longer Bobbable. Currently, not even an abstract method on a trait enforces anything on the receiving class. See: https://bugs.php.net/bug.php?id=75449 With that said, I have started some work on "Implicit Interfaces". It allows a trait to implement an interface in the sense that as long as any class actually implements a given interface, that class passes the type check (and instance_of() and ReflectionClass::implementsInterface(), is_a(), ...). Basically allowing duck typing. It has some similarities to https://wiki.php.net/rfc/protocol_type_hinting, you can check the current implementation on https://github.com/pmmaga/php-src/compare/implicit-interfaces Regards, Pedro

« previous php.internals (#101952) next »