Php 如何将变量值组合为常量值并作为常量输出
我想将变量值传递给常量值的一部分,以便使用phpspreadsheet在excel文件中进行格式化 1) 我定义了常量,并使用变量值调用常量值Php 如何将变量值组合为常量值并作为常量输出,php,phpspreadsheet,Php,Phpspreadsheet,我想将变量值传递给常量值的一部分,以便使用phpspreadsheet在excel文件中进行格式化 1) 我定义了常量,并使用变量值调用常量值 use PhpOffice\PhpSpreadsheet\Worksheet as Worksheet; $value ='PORTRAIT'; define("PORTRAIT", Worksheet\PageSetup::ORIENTATION_PORTRAIT); define("L
use PhpOffice\PhpSpreadsheet\Worksheet as Worksheet;
$value ='PORTRAIT';
define("PORTRAIT", Worksheet\PageSetup::ORIENTATION_PORTRAIT);
define("LANDSCAPE",Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
$orientation_constantvalue=constant($value);
$set_orientation =$sheet->getPageSetup()->setOrientation($orientation_constantvalue);
这个成功了
2) 我已将变量值直接传递给常量值的一部分
use PhpOffice\PhpSpreadsheet\Worksheet as Worksheet;
$value ='PORTRAIT';
$orientation_constantvalue="Worksheet\PageSetup::ORIENTATION_{$value}";
$set_orientation =$sheet->getPageSetup()->setOrientation($orientation_constantvalue);
这个不行
如果可能的话,我想将变量值直接传递给常量值,这是因为,我们可能不需要在常量中为每种类型的可能值定义常量。另一种方法是将值传递给常量并作为常量输出:
$orientation_constantvalue =constant('PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_'.$value);
$set_orientation =$sheet->getPageSetup()->setOrientation($orientation_constantvalue);
为此,
无法使用“将PhpOffice\PhpSpreadsheet\工作表用作工作表”;”
并将工作表\PageSetup::ORIENTATION\与$value连接起来
然后,它抛出的错误找不到常数
我们需要编写完整的引用并用常量中的变量连接
常量('PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION.'$value)
然后,它就可以工作了。与第一个示例中一样,您需要通过传递动态计算的名称,使用
constant
函数获取类常量的值。同时,使用类别名,您需要像本文中建议的那样稍微修改一下调用
这是我做的测试。我没有安装您的库,所以我从头开始创建了一些东西作为可重用的最小示例
我首先创建了class.php
,其中包含一个名称空间的类:
是的,很好用。谢谢。但是,需要编写完整的参考PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION.
。因为,我们已经将PhpOffice\PhpSpreadsheet\Worksheet定义为工作表
。仍然无法为此目的使用工作表编辑发布的符合您所有要求的内容(增量a需要调用hack in常量
)是,它工作得很好,我是这样做的<代码>$orientation\u constantvalue=constant(工作表::类。“\PageSetup::orientation\uu{$value}”)代码>。感谢您提供信息链接、答案和时间。我浏览了那个链接中的内容。