您的问题有两种方法。
首先,让我向您展示Robert C Martin 的Clean Code book 的引述(第 3 章:函数。函数参数 - 参数对象,第 43 页):
当一个函数似乎需要两个或三个以上的参数时,很可能其中一些参数应该被包装到它们自己的类中。
如我所见,您的值代表 RGB 颜色。为什么不把它包装成一个类呢?
class RGB
{
private $blue;
private $green;
private $red;
public function __construct($red , $green , $blue)
{
$this->red = $red;
$this->green = $gree;
$this->blue = $blue;
}
/** others necesary methods **/
}
随心所欲地使用:
$my_color = new RGB(88, 38, 123);
$myPdf->setFillColor($my_color);
如果您需要使用其他类型的颜色系统,只需使用一个界面:
interface Color { }
RGB实现颜色
class RGB implements Color
和一个新的颜色系统:
class CMYK implements Color
{
private $cyan;
private $magenta;
private $yellow;
private $black;
public function __construct($cyan , $magenta , $yellow , black)
{
$this->cyan = $cyan;
$this->magenta = $magenta;
$this->yellow = $yellow;
$this->black = $black;
}
}
PDF 方法只需要接受一个实现 Color 的类:
public function setFillColor(Color $color)
第二种方法,它不适合面向对象,但使用PHP >= 5.6或call_user_func_array的函数参数语法来传递可变数量的参数。我不能在您的示例中推荐(出于其他目的可能是一个好主意),但它存在。