Skip to content

danielefavi/php-fluent-api-class

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 

PHP Fluent API Class

FluentApi is a PHP class that helps you to create a fluent API interface.

Please find the full article at https://www.danielefavi.com/blog/php-fluent-api-class

Usage

1. Extend the class FluentApi in your class
class FluentMath extends FluentApi
{

}
2. Declare your functions

Declare the functions that are chainable with an underscore _.

In the example below the functions _add and _subtract can be chained and plus they can be called statically or not (please check the example of usage).

class FluentMath extends FluentApi
{
    private $result = 0;

    protected function _add($num)
    {
        $this->result += $num;

        return $this;
    }

    protected function _subtract($num)
    {
        $this->result -= $num;

        return $this;
    }

    public function result()
    {
        return $this->result;
    }
}
3. Example of usage
$res1 = FluentMath::add(5)
    ->add(3)
    ->subtract(2)
    ->add(8)
    ->result();

$res2 = FluentMath::subtract(1)->add(10)->result();

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages