Skip to content

Instantly share code, notes, and snippets.

@finagin
Created April 19, 2018 07:25
Show Gist options
  • Save finagin/fbf058e8a166b5e768b614fef0b4630d to your computer and use it in GitHub Desktop.
Save finagin/fbf058e8a166b5e768b614fef0b4630d to your computer and use it in GitHub Desktop.
<?php
if (!function_exists('optional')) {
function optional($instance, $default = null)
{
return new class($instance, $default = null)
{
private $instance;
private $default;
public function __construct($instance, $default = null)
{
$this->instance = $instance;
$this->default = $default;
}
public function __get($name)
{
return $this->instance->{$name} ?? $this->default;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment