1. Go to this page and download the library: Download patchlevel/enum library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
patchlevel / enum example snippets
declare(strict_types=1);
namespace Patchlevel\Enum\Example;
use Patchlevel\Enum\Enum;
/**
* @psalm-immutable
*/
final class Status extends Enum
{
private const CREATED = 'created';
private const PENDING = 'pending';
private const RUNNING = 'running';
private const COMPLETED = 'completed';
public static function created(): self
{
return self::get(self::CREATED);
}
public static function pending(): self
{
return self::get(self::PENDING);
}
public static function running(): self
{
return self::get(self::RUNNING);
}
public static function completed(): self
{
return self::get(self::COMPLETED);
}
}
declare(strict_types=1);
namespace Patchlevel\Enum\Example;
$status = Status::completed();
if ($status === Status::completed()) {
echo "That's working";
}
// or use as typehint
function isFinished(Status $status): bool {
return $status === Status::completed();
}
echo isFinished($status) ? 'yes' : 'no';
// or with the new php8.0 match feature:
$message = match ($status) {
Status::created() => 'Process created',
Status::pending() => 'Process pending',
Status::running() => 'Process running',
Status::completed() => 'Process completed',
default => 'unknown status',
};
echo $message; // Process completed