Skip to content

Instantly share code, notes, and snippets.

@richardDobron
Created November 3, 2023 20:39
Show Gist options
  • Save richardDobron/e1d004744746c379e639a483c51e57b3 to your computer and use it in GitHub Desktop.
Save richardDobron/e1d004744746c379e639a483c51e57b3 to your computer and use it in GitHub Desktop.
<?php
/**
* A utility that accepts a value and throws an Exception if the value is null,
* otherwise it returns the value.
*
* @throws Exception
*/
function nullthrows(mixed $value, string $message = null): mixed
{
if ($value !== null) {
return $value;
}
throw new Exception($message ?: 'Got unexpected null value.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment