如何从存储在变量中的字符串调用 PHP 属性

How to call PHP property from string stored in a Variable

我正在使用 maatwebsite/excel 包,我想动态传递不同的文件类型作为第二个参数。
See function here

下面是变量:

$fileType = $request->input('fileType', 'xlsx');
$writerType = Excel::$fileType;

但我收到错误:

Access to undeclared static property: Maatwebsite\Excel\Excel::$fileType

我尝试使用大括号但不起作用:

Excel::${"fileType"};

如何传递变量?谢谢!

 return Excel::create('PatientList',function($excel) use ($variable){

            $excel->sheet('List',function($sheet) use ($variable){
                $sheet->fromArray($variable);
            });

        })->download($type);

当您从视图中下载 file.pass $type 时,请像这样使用。

您正确传递了变量。但是你的问题是 Maatwebsite\Excel\Excel contains constant XLSX and it's not a property. Constants can be accessed staticly Excel::XLSX or dynamicly using constant 函数:

     $fileType = $request->input('fileType', 'xlsx');
     $writerType = constant('Excel::' . strtoupper($fileType));