On Wed, Dec 17, 2025 at 12:15 AM Yousha Aleayoub <[email protected]> wrote:
> *@Larry Garfield*, about lack of generics in PHP you are right, but my > proposal is NOT trying to solve generics, it’s trying to solve " > *fragmentation*"... > > Right now, every library reinvents its own Result/ErrorBag. Even if > imperfect, a shared interface (like PSR‑3) would reduce duplication and > improve interoperability. I'm not sure I can see a clear path for this PSR as written to be used by the libraries you mentioned, but that can be sorted out in draft phase. > Also runtime overhead is minimal compared to the clarity gained(specially > with current hardware and I/O). I see this as a "baseline contract" rather > than a perfect solution; similar to how PSR‑3 gave us a common logger > interface "without solving structured logging" That's fair, I think Larry is just responding to your "Zero runtime overhead" claim. > > *@**Korvin Szanto*, > Thanks, AFAIK PHP-FIG already standardizes APIs that rely on phpdoc > typing and runtime contracts. (mostly) > This PSR follows the same precedent. Like: > In PSR-7 we have ServerRequestInterface::getParsedBody(): mixed > PSR-7 was created at a time when PHP had no return types. If it were being created today `getParsedBody` would certainly return `null|array|object`, though I do get your point that this interface existed in a time where typing wasn't available and it relied on docblocks to narrow return types. > In PSR-18 we have sendRequest() that returning interface without generics > Return types can be narrowed to a subtype without violating LSP. So an implementor could implement `public function sendRequest(): SpecificResponse` as long as `SpecificResponse` implements `ResponseInterface`. That's quite different from what we're talking about here unless you're expecting an implementor to add many different Result types. > etc... > > So since PHP lacks native generics, so this new PSR follows existing FIG > practice: phpdoc generics plus runtime invariants. > A Result is either success or failure by construction; mixed typing is not > allowed. > Implementors are not expected to create a class per result—one > implementation per library is sufficient. > I’ll align the META to make this explicit and consistent with the PR. > So to be clear the expectation is that a user MUST add a docblock with `@return ResultInterface<SuccessTypes, ErrorTypes>`, correct? I think the challenge with this today is the ambiguity around how `@template` works and the potential for future changes to make the generics we define today in docblocks overly cumbersome or wholesale unusable. In comparison, PSR-7 didn't face any risk that community libraries would change how `Foo|Baz|null` was interpreted. > > Added a section in meta document: > https://github.com/php-fig/fig-standards/pull/1344/files#diff-99b9802e69456918264b6be5f78aa91cffedf996d1fe910b0e817c4d30d9be72R81 > > > On Tuesday, December 16, 2025 at 11:30:42 PM UTC+3:30 Korvin Szanto wrote: > > On Sun, Dec 14, 2025, 10:34 AM Yousha Aleayoub <[email protected]> wrote: > > Hello, > > I’m submitting for discussion and vote a new PSR proposal to address a > long-standing gap in PHP’s *error-handling* ecosystem: > > Standard Interfaces for Recoverable Errors and Operation Results > *(Working title: PSR-XX Error and Result Interfaces)* > > *The problem* > PHP *forces *developers to choose between two flawed patterns for > non-exceptional failures: > > > - Throwing exceptions for expected outcomes (like validation errors, > cache misses) -> breaks control flow, hurts performance, and conflates > recoverable vs. unrecoverable errors. > - Returning ambiguous values (null, false, ['error' => ...]) → > destroys type safety and forces brittle checks across the call stack. > > Frameworks and libraries reinvent this wheel constantly: > > - Laravel’s ValidationException > - Symfony’s ConstraintViolationList > - Guzzle’s TransferException + status codes > - Custom Result<bool, string> classes in libraries... > > There is no interoperable, type-safe way to return structured, recoverable > errors. > > *The Solution* > This PSR will propose three minimal, zero-dependency interfaces: > > > 1. ResultInterface: a sealed union for success/failure states > 2. SuccessInterface: wraps a successful value > 3. FailureInterface: wraps structured error data (code, message, > context) and optionally a Throwable > > *Key features* > > - No exceptions required for expected failures! > - Immutable, readonly objects > - Pattern-matchable via isSuccess()/isFailure() > - Composable with PSR-3 (logging), PSR-11 (containers), *and async > runtimes* > - Zero runtime overhead – pure interfaces, no DI or middleware > > Thanks for your time. > > -- > You received this message because you are subscribed to the Google Groups > "PHP Framework Interoperability Group" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion visit > https://groups.google.com/d/msgid/php-fig/ed4abd14-7f0c-4559-988c-8afd04538be3n%40googlegroups.com > <https://groups.google.com/d/msgid/php-fig/ed4abd14-7f0c-4559-988c-8afd04538be3n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > > Thanks for putting this and the PR together. I definitely think this would > be a good thing to have, but I agree with Larry that the lack of generics > makes this challenging. > > The solution you provide in this thread is different than whats presented > in https://github.com/php-fig/fig-standards/pull/1344 . Can you clarify > what you intend to do to avoid the mixed typing for both success and > failure? Are you thinking an implementor would provide a different result > class for each possible result? > > Thanks, > Korvin > > -- > You received this message because you are subscribed to the Google Groups > "PHP Framework Interoperability Group" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion visit > https://groups.google.com/d/msgid/php-fig/2f7364d1-1c8d-48c0-b8f3-6ee792db2ae3n%40googlegroups.com > <https://groups.google.com/d/msgid/php-fig/2f7364d1-1c8d-48c0-b8f3-6ee792db2ae3n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > That all said, I think there's enough here that this could pass an entrance vote and some of the questions around generics and enforcing invariance can be answered in the draft phase over time. If we're lucky, usable generics will land in PHP before this PSR is finalized. -- You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/php-fig/CANeXGWV3K-9ZKoDPRu5nEuUT-_t6jzV8rCTcCt7J7QKmp%3D_E9Q%40mail.gmail.com.
