Skip to content

Instantly share code, notes, and snippets.

@emmadesilva
Last active December 5, 2023 08:37
Show Gist options
  • Select an option

  • Save emmadesilva/c64628fa48d07ffd1568c1189485921b to your computer and use it in GitHub Desktop.

Select an option

Save emmadesilva/c64628fa48d07ffd1568c1189485921b to your computer and use it in GitHub Desktop.
Idea for a strongly typed systen
<?php
declare(strict_types=1);
final class Types
{
protected static bool $strict = false;
public static function string(mixed $input): string
{
if (self::$strict && ! is_string($input)) {
self::throwTypeError('string', $input);
}
return (string) $input;
}
protected static function throwTypeError(string $type, mixed $input)
{
return throw new TypeError(sprintf('Argument 1 passed to %s must be of the type %s, %s given',
__METHOD__, $type, gettype($input))
);
}
}
final class Cast
{
public static function toString(mixed $input): string
{
return (string) $input;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment