Re: Alternatives for encapsulation when using composition instead of inheritance

From: Date: Thu, 26 Aug 2021 18:30:02 +0000
Subject: Re: Alternatives for encapsulation when using composition instead of inheritance
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-115863@lists.php.net to get a copy of this message
On 26/08/2021 14:47, Olle Härstedt wrote:
Don't know if this already exists, but maybe we could consider brainstorming or gather alternatives for different types of encapsulation when using composition?
Although not quite equivalent to your suggestions, these threads on making delegation to an encapsulated object might be interesting to you: *  https://externals.io/message/103353 * https://github.com/php/php-src/pull/5168 If we had something like that, then perhaps there could be a "delegatable" visibility, which was the equivalent for "protected", but when delegating rather than inheriting, e.g. class Foo {
    private int $a = 1;
    delegatable int $b = 2;
} class Bar {
    private delegate Foo $foo;
    public function __construct(Foo $foo) {
        $this->foo = $foo;
        echo $this->a; // Error: can't access private property of a different class
        echo $this->b; // allowed: access is to a "delegatable" property, in a "delegated" context
    }
} -- Rowan Tommins [IMSoP]

« previous php.internals (#115863) next »