---------- Forwarded message --------- From: Olle Härstedt <olleharst...@gmail.com> Date: Fri, 18 Sep 2020, 11:23 Subject: Re: [PHP-DEV] Namespace-private class properties To: Michał Marcin Brzuchalski <michal.brzuchal...@gmail.com>
On Fri, 18 Sep 2020, 06:15 Michał Marcin Brzuchalski, < michal.brzuchal...@gmail.com> wrote: > Hi Olle, > > czw., 17 wrz 2020, 14:28 użytkownik Olle Härstedt <olleharst...@gmail.com> > napisał: > >> (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: >> 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 >> >> 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? >> > > I say it's not possible. There is no control over use when you pass > reference. It is easy to load class in "child" namespace which extracts > references and pass them wherever. > > Keep in mind that namespace on PHP is just a class/funtion/const name > prefix and nothing more nothing less. While it may work in languages with > support of modules in PHP there is no such thing. > > Cheers, > Michał Marcin Brzuchalski > Hm, does that mean that PHP doesn't know which namespace it's in in any given moment? Is there no "context" tracking in the compiler? >