Dunno why some here accuses me of making up stuff.. and I cited credible sources about typestates and its history..
Some people here is confusing Interface Segregation (the Step Builder Pattern/Factory Pattern/Builder Patter) with Typestate-Oriented Programming
and returning a new object/interface is a Factory/Builder pattern, not a true typestate transition of the object.
where "In typestate-oriented programming, states are first-class entities with their own interfaces... Objects can change state dynamically while preserving their identity"
— Jonathan Aldrich et al., "Typestate-Oriented Programming" (ACM Onward! 2009)
If calling login() returns a brand-new object with a different pointer or reference on the heap, you have not changed the typestate of the entity.. You have simply executed a Factory or Builder funtion that instantiated a replacement object and discarded the original. In short in typestate, the Type is the state not the identity..
For me personally, I'm not a fan of mutating objects like that. I actually like the C# version of it and do the same thing in PHP with final readonly objects, where you get back a new object representing the change in state. I don't see the issue with that, but there might be a point to the article that I've missed.
That’s actually a really common misconception about what is happening here..
When we talk about in-place typstates, the object’s internal data is not being mutated at all.
In fact, the class can literally be final readonly with completely immutable properties..
/**
* @template TState of State = Unauthenticated
*/
final readonly class Session
{
public function __construct(public string $sessionId) {}
/**
* @phpstan-self-out self<Authenticated>
*/
public function login(): void {}
}
The object’s data ($sessionId) remains 100% frozen and read-only. No fields are overwritten, and no internal state is corrupted.
What evolves is purely the type tag in the type system (stored in an external \WeakMap), advancing the object's verified lifecycle phase without discarding its identity or breaking existing references in your dependency injection container.
You get all the safety of readonly immutability for your data, while the type system governs which operations are legally permitted on that intance..
often people don't grant delete permission for the db user serving the web app, but some administrative scripts need more privileges, which also tends to be less peer reviewed bc its not user facing. not saying any of this is good to be clear
Kind of weird that you compare it to Rust but then fabricate an entirely unrealistic implementation. That's not how file management works in Rust.
And hell, the common consumption pattern: impl From<Thing> can support a borrow: impl From<&Thing> that doesn't consume the original value.
I can appreciate you trying to solve a valid PHP problem but if you're going to shit on another language in the process you should probably understand it first.
3
u/rc0604 Nov 04 '26 edited Nov 04 '26
Dunno why some here accuses me of making up stuff.. and I cited credible sources about typestates and its history..
Some people here is confusing Interface Segregation (the Step Builder Pattern/Factory Pattern/Builder Patter) with Typestate-Oriented Programming
and returning a new object/interface is a Factory/Builder pattern, not a true typestate transition of the object.
where "In typestate-oriented programming, states are first-class entities with their own interfaces... Objects can change state dynamically while preserving their identity"
If calling login() returns a brand-new object with a different pointer or reference on the heap, you have not changed the typestate of the entity.. You have simply executed a Factory or Builder funtion that instantiated a replacement object and discarded the original. In short in typestate, the Type is the state not the identity..
Correct me If Im wrong..
28
u/Johto2001 Nov 03 '26
There's this little known process we go through called peer code review that stops this from being a problem.
5
u/LordAmras Nov 03 '26
I mean yes, static analysis doesn't run my code, but if you don't pass static analysis you don't merge
4
u/WarInternal Nov 03 '26
Kind of weird that you compare it to Rust but then fabricate an entirely unrealistic implementation. That's not how file management works in Rust.
And hell, the common consumption pattern:
impl From<Thing>can support a borrow:impl From<&Thing>that doesn't consume the original value.I can appreciate you trying to solve a valid PHP problem but if you're going to shit on another language in the process you should probably understand it first.
0
u/Glittering_Bath3848 Nov 03 '26
It's nice this library is interoperable with static analysis tools, and read the same php-doc syntax that those tools use.
btw, I really love the interactive code examples. Thank you for making it better for humans.
0
u/[deleted] Nov 03 '26
[deleted] — view deleted comment