### Install prewk/result using Composer Source: https://github.com/prewk/result/blob/master/readme.md This snippet shows the Composer command to install the prewk/result library, which is necessary for using the Result type in your PHP project. ```bash composer require prewk/result ``` -------------------------------- ### PHP Helper Functions for Result Construction Source: https://github.com/prewk/result/blob/master/readme.md Provides examples of global helper functions (`ok` and `err`) that simplify the creation of `Ok` and `Err` instances, making the code more concise. ```php ok(); // new Prewk\Result\Ok(null); ok($val); // new Prewk\Result\Ok($val); err($e); // new Prewk\Result\Err($e); ``` -------------------------------- ### Composer Autoload Configuration for Helpers Source: https://github.com/prewk/result/blob/master/readme.md Shows how to configure `composer.json` to automatically load the helper functions provided by the prewk/result library. ```json { "autoload": { "files": ["vendor/prewk/result/helpers.php"] } } ``` -------------------------------- ### Basic Usage of Result Type in PHP Source: https://github.com/prewk/result/blob/master/readme.md Demonstrates how to create and return Result objects in PHP, representing either a successful outcome with a value (Ok) or an error state (Err). This is fundamental for implementing the Result pattern. ```php use Prewk\Result; use Prewk\Result\{Ok, Err}; function someApiCall(): Result { // ... if ($apiCallSuccesful) { return new Ok($results); } return new Err($error); } function anotherApiCall(): Result { // ... if ($apiCallSuccesful) { return new Ok($results); } return new Err($error); } ``` -------------------------------- ### Handling Result Objects in PHP Source: https://github.com/prewk/result/blob/master/readme.md Illustrates common methods for interacting with Result objects, including unwrapping values with a fallback (unwrapOr), chaining operations with error handling (orElse), and expecting a successful result with a custom exception (expect). ```php // Fallback to value $value = someApiCall()->unwrapOr(null); // Fallback to result and throw an exception if both fail $value = someApiCall()->orElse(function($err) { return anotherApiCall(); })->unwrap(); // Throw custom exception on error $value = someApiCall()->expect(new Exception("Oh noes!")); ``` -------------------------------- ### Immediate Evaluation of 'or' and 'and' in PHP Result Source: https://github.com/prewk/result/blob/master/readme.md Highlights a potential gotcha where the `or` and `and` methods on Result objects are evaluated immediately, potentially causing unintended side effects by executing all chained operations regardless of success or failure. ```php // This will call all three api calls regardless of successes/errors $this ->apiCall() ->or(anotherApiCall()) ->and(thirdApiCall()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.