badcow / fluent-interface
此包已被废弃,不再维护。未建议替代包。
将任何非最终类的无返回值方法转换为链式、流畅的接口。
v1.0.0
2015-04-06 21:15 UTC
Requires
- php: >=5.3.9
This package is auto-updated.
Last update: 2022-02-01 12:24:05 UTC
README
将任何非最终类的无返回值方法转换为链式、流畅的接口。
免责声明
这是一个玩笑。我是在与不喜欢流畅接口的高级开发人员争论后创建了这个库。如果您能想到这个库的合法用途,我希望知道。
安装
使用composer
//composer.json
{
"require": {
"badcow/fluent-interface": "dev-master"
}
}
基本用法
非流畅类
<?php
//sample_class.php
namespace Sour\Milk;
class FooBar
{
/**
* @var string
*/
private $firstName;
/**
* @var string
*/
private $lastName;
/**
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* @param string $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
/**
* @param string $lastName
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
}
创建流畅代理
<?php
use Badcow\FluentInterface\FluentInterface;
//Set the directory where to store the proxy classes
$fi = new FluentInterface(__DIR__ . '/Proxies');
//Create a proxy class. The second parameter forces the recreation of the file.
$proxy = $fi->create('Sour\Milk\Foobar', true);
$foobar = new $proxy();
//Class is now a fluent interface
$foobar->setFirstName('Sam')->setLastName('Williams');
echo $foobar->getFirstName() . ' ' . $foobar->getLastName();
运行测试套件
测试套件使用PHPUnit运行。
$ cd /path/to/badcow-fluent/interface/
$ composer install
$ phpunit .