Optional
A library to reduce the code required for null-checking.
Install / Use
/learn @typedphp/OptionalREADME
Optional
This library is inspired by the code of Johannes Schmitt and the writing of Igor Wiedler. It is intended to reduce the code required for null-checking. Part of TypedPHP.
Examples
<?php
require("vendor/autoload.php");
use TypedPHP\Optional\Optional;
class Foo
{
public function hello()
{
return new Bar();
}
}
class Bar
{
public function world()
{
return "hello world";
}
}
$optional = new Optional(new Foo());
$optional->hello()->world()->value(); // "hello world"
<?php
require("vendor/autoload.php");
use TypedPHP\Optional\None;
$none = new None();
$none->hello()->world()->value(); // null
<?php
require("vendor/autoload.php");
use TypedPHP\Optional\None;
$none = new None();
$none
->none(function() { print "none"; }); // "none" printed
->value(function($value) { print $value; }); // $value not printed
use TypedPHP\Optional\Optional;
$optional = new Optional("hello world");
$optional
->none(function() { print "none"; }); // "none" not printed
->value(function($value) { print $value; }); // "hello world" printed
Once an Optional method call returns an empty value, it is transformed into a None.
Installation
❯ composer require "typedphp/optional:*"
Testing
❯ composer create-project "typedphp/optional:*" .
❯ vendor/bin/phpunit
Related Skills
node-connect
344.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
96.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
