【发布时间】:2018-01-24 14:18:55
【问题描述】:
有时,特别是对于回调函数或继承/实现情况,我不想在方法中使用一些参数。但是方法接口签名需要它们(我不能更改签名,假设这是通过 Composer 需要的东西)。示例:
// Assuming the class implements an interface with this method:
// public function doSomething($usefull1, $usefull2, $usefull3);
public function doSomething($usefull, $useless_here, $useless_here) {
return something_with($usefull);
}
// ...
在其他一些语言(比如 Rust)中,我可以显式忽略这些参数,这使代码(和意图)更具可读性。在 PHP 中,可能是这样的:
public function doSomething($usefull, $_, $_) {
return something_with($usefull);
}
这在 PHP 中可行吗?我错过了什么吗?
旁注:它不仅用于尾随参数,还可以在函数声明中的任何位置
【问题讨论】:
标签: php arguments custom-function