Re: Namespace-private class properties

From: Date: Thu, 17 Sep 2020 22:13:41 +0000
Subject: Re: Namespace-private class properties
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-111896@lists.php.net to get a copy of this message
> On Sep 17, 2020, at 8:28 AM, Olle Härstedt <olleharstedt@gmail.com> wrote: > > (NB: This idea is NOT about namespace visibility for classes, interfaces or > traits (already discussed here: > https://wiki.php.net/rfc/namespace-visibility). > It's about adding a *new* visibility qualifier, call it "internal", to > make properties > private inside a namespace. The purpose is to make composition less > fragile by not > exposing more than necessary.) > > (NB: This feature exists in Swift, described here: > ðlÓg > ø!·=m+å`‡https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html) > > We have public, protected and private. Since PHP has no module system, we > have no qualifier to mark a class property as "private for this module". > One solution to this could be to add a new qualifier "internal", to make > properties public within the current namespace. > > Use-cases: > > * Split data and behaviour into different files and classes > * Safely skip getters and setters without risk of properties being abused > by client code +1 This might be able to address a use-case I have wanted for a while, which is to have traits with properties (and methods) that are not accessible with the class that uses the trait. So if a trait is in a different namespace than the using class, the using class would not be able to access these "internal" properties. Or at least I think that could work? -Mike > > Tiny example: > > ``` > namespace App\User; > class User { > internal $username; > internal $password; > } > > namespace App\User\Services; > class ProcessUser { > // Child namespace has access to internal properties, > // but App\ has not. > } > ``` > > Possible? > > Olle >

« previous php.internals (#111896) next »