承接 thejawker/chainable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thejawker/chainable

Composer 安装命令:

composer require thejawker/chainable

包简介

Exposes a Class that makes method chaining fluent and easy.

README 文档

README

Are you tired of using packages that require you to write code like the following:

$someClass = new SomeClass();
$someClass->someMethod();
$someClass->someOtherMethod();

// Now you can just write it like this.
new Chain(SomeClass::class)
    ->someMethod()
    ->someOtherMethod();
    
// Or even easier
ch(SomeClass::class)
    ->someMethod()
    ->someOtherMethod();

Installation

Require the package from Composer:

composer require thejawker/chainable

Usage

Basic Usage

You can use the Chain in two easy different ways, depending on your liking or necessity.

// Through passing the Class's classname.
$someClass = new Chain(SomeClass::class);

// Or through passing the actual instance
$someClass = new Chain(new SomeClass($withParams))

Getting Properties

Getting properties is just the same, these won't return $this.

$property = new Chain(SomeClass::class)->property;

Escaping Chaining

Often methods do need to return an actual value, otherwise we can stop programming all together. Chain will allow you to easily turn off the chaining behaviour. This can be done very easily.

$someClass = new Chain(SomeClass::class)->someMethod()
$property = $someClass->escape()->getValue() // will return the original value

You can also unescape to get the Chainable functionality back. To be honest I can't find a use case for this yet, but hey! You never know!

$someClass = new Chain(SomeClass::class)->someMethod();
$sameInstance = $someClass->escape();
$sameInstance->unescape()->otherMethod();

Get The Original Instance

Sometimes it is needed to return the original instance because some code might be checking if it is an instanceof a class. You can easily chain the ->instance() method on there and you get the original instance back, not a clone, the actual thing. Note: you can't go back from here.

public function calculate() 
{
    return new Chain(new LegacyClass)
        ->someMethod()
        ->setSome('stuff')
        ->maybeMore()
        ->instance();
}

Wanna Go Pro?

I've also included a convenient shortcut for those who think instantiating a Chain is too much work. The following will yield exactly the same result but then just with much more ease.

ch(LegacyClass::class)
    ->also()
    ->works('yay!')
    ->fuckYess();

Testing

I've included some tests, feel free to send me pull-requests if you see room for improvement.

composer test

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 597
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固