Namespace-private class properties
| From: | Olle Härstedt | Date: | Thu, 17 Sep 2020 12:28:06 +0000 |
| Subject: | Namespace-private class properties | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-111876@lists.php.net to get a copy of this message | ||
(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?
Olle