Skip to content

Using “when” and “unless” in your own classes

Notifications You must be signed in to change notification settings

muath-ye/PHP-when-unless

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

When and Unless

For this to work, we can create a simple trait so we are able to use the helpers on anything we want. These methods will take the condition, and call a callable if it evaluates to true. For unless, we can just call when() but with the condition flipped:

<?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);
    }
}

And we can fully do things like the above. Since it receives a callable, we can even call public static methods from elsewhere just because we can.

$transport->when($express, [Courier::class, 'setExpress'])

About

Using “when” and “unless” in your own classes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages