Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将变量的值用作属性_Php_Phpexcel - Fatal编程技术网

Php 如何将变量的值用作属性

Php 如何将变量的值用作属性,php,phpexcel,Php,Phpexcel,我编写了一个使用PHPExcel的PHP脚本。它应该使用配置文件来创建电子表格。当我尝试应用样式时,我的问题出现了 我根据配置文件中给出的信息创建了一个样式数组。现在,当涉及到例如单元格或边框的对齐时,您可以使用 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER 'horizontal' => PHPExcel_Style_Alignment::$variableWithPropertyName 其中,水平_中

我编写了一个使用PHPExcel的PHP脚本。它应该使用配置文件来创建电子表格。当我尝试应用样式时,我的问题出现了

我根据配置文件中给出的信息创建了一个样式数组。现在,当涉及到例如单元格或边框的对齐时,您可以使用

'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
'horizontal' => PHPExcel_Style_Alignment::$variableWithPropertyName
其中,
水平_中心
是所需的属性。现在,我将这个属性名存储在一个变量中,不能使用

'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
'horizontal' => PHPExcel_Style_Alignment::$variableWithPropertyName
但是我需要使用变量的值,因为可以有不同的属性


有什么想法吗?

您应该能够使用反射API检索值,如下所示:

$property = new ReflectionProperty('PHPExcel_Style_Alignment', $variableWithPropertyName); 
然后可以像这样检索值:

'horizontal' => $property->getValue();
应该返回一个可能的对齐值数组,按其名称索引,以便您可以设置

'horizontal' => $alignmentsArray[$variableWithPropertyName]

其中,
$variableWithPropertyName
是您的值,如
水平\u中心

我忘了提到,当然出现了致命错误“访问未声明的静态属性:PHPExcel\u样式\u对齐:$layoutValues!”$layoutValues具有所需的属性名称。我将在这里进行粗略猜测,并假设您将
$layoutValues
声明为非静态。您可以使用
$this
访问它。比使用ReflectionProperty更好,因为这些都是类常量