Search⌘ K
AI Features

The Railway Pattern

Explore the Railway pattern as a functional approach to error handling in PHP. Understand how to manage success and failure in function composition using the Either monad and promises. This lesson helps you implement robust and composable error handling alternatives to exceptions.

Background to the Railway pattern

Scott Wlaschin, a prominent promoter of functional programming and a vocal leader in the F# community, advanced the Railway pattern, a formula for error handling using controlled function composition. This pattern is a novelty only in name, for it’s nothing more than an elaborate error handling with sum types such as the Either monad.

When dealing with real-world problems, it’s common to deal with both success and failure scenarios. The Railway pattern encourages us to conceive these as two switches on the track. Furthermore, according to the rubric, both pathways should provide similar values to aid in composition. This requires two-switch functions as a prerequisite.

If we look at the functionUntitledConcept1 from the ...