食物 / shoarma
部分函数、闭包和所有种类的魔法
v1.2.0
2017-11-23 14:12 UTC
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.7
- phpunit/phpunit: ^6.4
This package is auto-updated.
Last update: 2019-03-01 01:46:49 UTC
README
在函数中带回 fun
fun(class $object, string $method)
返回 \Closure
用于替换 [$object, $method]
的简写,当从错误上下文中调用时也会抛出异常
this(string $method)
返回 \Closure
用于替换 fun($this, $method)
的简写,通过魔法(debug_backtrace
)将收集正确的 this 对象
partial(callable $call, $arguments)
返回 \Shoarma\Partial
为 $call
创建一个部分,可以使用 \Shoarma\arg
和 \Shoarma\args
来替换参数,例如:
<?php
use function \Shoarma\partial;
use function \Shoarma\arg;
$func = function ($one, $two, $three) {
echo "1. {$one}\n2. {$two}\n3. {$three}\n";
};
$partial = partial($func, [arg(1), "2", arg(0)]);
// This will now print "1. 1\n 2. 2\n3. 3"
$partial(3, 1);
arg(int $offset)
返回 \Shoarma\Partial\Argument
为部分选择 1 个参数
args(int $offset, $length = null)
返回 \Shoarma\Partial\Argument
为部分选择一系列参数,如果 $length
为空,则从 $offset
开始选择所有内容
wrap(callable $call, callable $wrapper)
返回 Shoarma\Wrap
将返回 $wrapper
包装在 $call
之上
$wrapper
将被调用,并带有 \Shoarma\Wrap\Inside
对象围绕 $call
和其他参数
例如:
<?php
use \Shoarma\Wrap\Inside;
use function \Shoarma\wrap;
$func = function ($hello, $world) {
return "{$hello} {$world}\n";
};
$wrapper = wrap($func, function (Inside $next) {
// is the same as $next->callOriginal();
// Will use the arguments given to the wrapper
return $next();
// is the same as $next->callWith('Hello', 'World');
// so if you really need to call $next with no arguments you can use
// $next->callWith();
return $next('Hello', 'World');
});
// Will print "Goodbye mars"
echo $wrapper("Goodbye", "Mars");