On Thu, Mar 1, 2018 at 11:04 PM, Rowan Collins <rowan.coll...@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

Reply via email to