Re: Limit write access to object function parameters

From: Date: Mon, 08 May 2023 08:01:57 +0000
Subject: Re: Limit write access to object function parameters
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-120210@lists.php.net to get a copy of this message
2023-05-06 12:55 GMT+02:00, Olle Härstedt <olleharstedt@gmail.com>: > 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 Also related is Object.freeze from JS (I was told): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze Olle

« previous php.internals (#120210) next »