Limit write access to object function parameters
| From: | Olle Härstedt | Date: | Sat, 06 May 2023 10:55:05 +0000 |
| Subject: | Limit write access to object function parameters | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-120203@lists.php.net to get a copy of this message | ||
Heyo internalitos,
I was thinking of having the possibility to use
readonly (or any
other keyword) to make a function argument behave as if it was a
readonly object.
class Point
{
public int $x;
public int $y;
}
function doThing(readonly Point $p)
{
$p->x = 10; // Not allowed
}
In C# it's called in/out/ref types:
https://www.pluralsight.com/guides/csharp-in-out-ref-parameters
The main use-case is to not give away more access than you have to,
and being able to state your intent in the function signature.
Another alternative would be to allow properties in interfaces, and
then define a readonly-like interface:
interface ReadonlyPoint
{
public readonly int $x;
public readonly int $y;
}
function doThing(ReadonlyPoint $p)
{
$p->x = 10; // Not allowed
}
No idea about practical feasability.
Since arrays are value types, they're not really relevant for this
suggestion. Same goes for string, int, etc.
Regards
Olle