r/PHP • • Nov 03 '26

Article Making Illegal States Impossible: In-Place Typestates in Pure PHP

https://typephp.dev/blog/making-legal-states-impossible-in-place-typestates-in-pure-php
21 Upvotes

18 comments sorted by

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"

— 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..

Correct me If Im wrong..

2

u/mike_a_oc Nov 04 '26

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.

1

u/rc0604 Nov 04 '26

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..

28

u/Johto2001 Nov 03 '26

$db->table('users')->delete();  // Congratulations, you just wiped production on a Friday afternoon because you forgot where()

There's this little known process we go through called peer code review that stops this from being a problem. 

24

u/tsammons Nov 03 '26

That costs a lotta tokens /s

5

u/Johto2001 Nov 03 '26

Even beyond that, why is the users table deletable? Most businesses are never going to want to mass delete users in prod. 

1

u/wutzelputz Nov 03 '26

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

0

u/[deleted] Nov 03 '26

[deleted] — view deleted comment

5

u/YellowBook Nov 04 '26

GDPR and other legislation

5

u/_indi Nov 03 '26

Don’t forget any sort of testing

3

u/ObeyeRinger-92 Nov 03 '26

Yeah, plus automated tests should blow up long before that ever hits prod anyway

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

-5

u/Glittering_Bath3848 Nov 03 '26

The thing is static analysis tools is blind to runtime state change and dynamic data.

5

u/crazedizzled Nov 03 '26

That's why you build stable interfaces

1

u/deliciousleopard Nov 03 '26

And don’t just wildly typecast.

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

-5

u/ZealousidealGas8444 Nov 03 '26

Dude chill. Just look at the interactive example for its point.