php - 类和扩展类中的 PHP 'USE' 运算符
问题描述
我试图了解如何USE
在基类和扩展类中使用。我四处搜索,但我认为我没有正确的术语。
假设我的基类看起来像
namespace App\Classes;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Legend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
class ExcelReport
{
public $spreadsheet;
public function __construct()
{
$this->spreadsheet = null;
}
}
然后我扩展了那个课程
namespace App\Classes;
class MonthlyExcelReport extends ExcelReport
{
public $id;
public function __construct(int $id)
{
parent::__construct();
$this->id = $id;
}
public function build()
{
$reader = IOFactory::createReader('Xlsx');
}
}
我必须做什么才能IOFactory
在扩展类中调用以识别use PhpOffice\PhpSpreadsheet\IOFactory;
基类中存在的内容?
我目前收到此错误Class 'App\Classes\Gap\IOFactory' not found
,我不想use
在扩展类中重复所有这些语句。
解决方案
使用运算符用于“包含”一个类。如果您不使用“使用”运算符,则可以将其包含为“完整路径”。
在你的情况下:
$reader = IOFactory::createReader('Xlsx');
应该:
$reader = PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
推荐阅读
- javascript - 未在 react-native 中显示关键错误,创建自己的组件时
- java - 如何连接一定数量的数组?
- apache-spark - Bigquery“Getting IllegalArgumentException:Invalid Table ID”错误,同时使用查询将 bigquery 表的一部分读入 pyspark 数据帧
- roc - 如何设置 roc 曲线 pos_label !=0
- python - 如何保护烧瓶中的服务器端会话?
- macos - 无法从终端运行 curl SDK man 或 ruby
- flutter - 参数类型字符串?不能分配给参数类型“字符串”
- mysql - 不使用 MySQL 在线打开 SQL 数据库
- html - CSS使用户文本在表格中垂直选择
- javascript - 如何修复 Vue 3 中的 Vetur Linting 错误