Php 变量方法上的变量属性
我正在使用一个第三方软件包来处理图像。对于基本转换,它接受单个值:Php 变量方法上的变量属性,php,laravel-5,php-7,variable-variables,spatie,Php,Laravel 5,Php 7,Variable Variables,Spatie,我正在使用一个第三方软件包来处理图像。对于基本转换,它接受单个值: $image = $this->addMediaConversion('thumb'); $image->width(100); $image->height(100); 我正在构建的系统有一个抽象级别,我需要在配置文件中定义这些值。我以数组的形式加载配置文件。然后,我可以遍历配置文件中的值并生成各种转换 我的配置文件: return [ 'thumb' => [ 'width' =>
$image = $this->addMediaConversion('thumb');
$image->width(100);
$image->height(100);
我正在构建的系统有一个抽象级别,我需要在配置文件中定义这些值。我以数组的形式加载配置文件。然后,我可以遍历配置文件中的值并生成各种转换
我的配置文件:
return [
'thumb' => [
'width' => 100,
'height' => 100,
],
];
从该配置定义这些转换的代码:
$definitions = config('definitions');
foreach($definitions as $name => $keys) {
$image = $this->addMediaConversion($name);
foreach($keys as $key => $value) {
$image->$key($value);
}
}
这适用于单个值
但是,包中的方法对一个方法具有多个属性,例如:
$image = $this->addMediaConversion('thumb');
$image->fit(Manipulations::FIT_FILL, 560, 560);
有各种可用的方法具有各种不同的可接受属性。我正在寻找一个优雅的解决方案。我可以通过在配置文件中拥有一个值数组、检查类型、检查该数组的长度,然后传递正确的数字来实现它,但这既不可扩展,也不容易维护,也不优雅
配置:
return [
'thumb' => [
'fit' => [Manipulations::FIT_FILL, 560, 560]
]
];
代码:
什么是最好、最优雅的解决方案?请查看您必须使用call\u user\u func\u数组,如下所示:
foreach($image_definitions as $name => $keys) {
// Generate the conversion
$conversion = $this->addMediaConversion($name);
// Loop through and define the attributes as they are in the config, things like ->width(), ->height()
foreach ($keys as $key => $value) {
if (is_array($value)){
call_user_func_array(array($conversion, $key), $value);
} else {
$conversion->$key($value);
}
}
}
谢谢对于谷歌来说,这是一个困难的问题。
foreach($image_definitions as $name => $keys) {
// Generate the conversion
$conversion = $this->addMediaConversion($name);
// Loop through and define the attributes as they are in the config, things like ->width(), ->height()
foreach ($keys as $key => $value) {
if (is_array($value)){
call_user_func_array(array($conversion, $key), $value);
} else {
$conversion->$key($value);
}
}
}