Skip to content

Instantly share code, notes, and snippets.

@bubba-h57
Created October 30, 2023 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bubba-h57/22586c2fc2173b81027e4fb54d2452f9 to your computer and use it in GitHub Desktop.
Save bubba-h57/22586c2fc2173b81027e4fb54d2452f9 to your computer and use it in GitHub Desktop.
When/Unless Trait for PHP Objects
<?php
trait Conditionable
{
public function when($condition, $callable)
{
if ($condition) {
$callable($this, $condition);
}
return $this;
}
public function unless($condition, $callable)
{
return $this->when(!$condition, $callable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment