PHP';使用';类和扩展类中的运算符

PHP';使用';类和扩展类中的运算符,php,class,Php,Class,我试图理解如何在基类和扩展类中使用use。我到处找了找,但我认为我没有正确的术语 假设我的基类看起来像 namespace App\Classes; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Chart\Title; use PhpOf

我试图理解如何在基类和扩展类中使用
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
,以识别
使用PhpOffice\PhpSpreadsheet\IOFactory是否存在于基类中

我当前发现了这个错误,我不想在扩展类中重复所有那些
use
语句。

use操作符用于“包含”一个类。 如果不使用“use”操作符,则可以将其包含为“full path”

就你而言:

$reader = IOFactory::createReader('Xlsx');
应该是:

$reader = PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
使用运算符用于“包含”类。 如果不使用“use”操作符,则可以将其包含为“full path”

就你而言:

$reader = IOFactory::createReader('Xlsx');
应该是:

$reader = PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
TL;博士 名称空间允许您有两个名称相同但位于不同名称空间中的类

想象一下类
Animal\Bear\Claw
machine\Compactor\Claw
,在PHP引入名称空间之前,当我们需要像
Animal\u Bear\u Claw
machine\u Compactor\u Claw
这样丑陋的类名时,名称空间是可以实现的

现在,当您实例化或使用这些类时,您不想总是在扩展中说

new \Animal\Bear\Claw();
您希望能够说:“我在一个动物工厂模式的上下文中,基本上会作用于动物名称空间下的类,而不是机器的类”

所以你说:

use Animal\Bear\Claw;

new Claw();

甚至还有别名

use Animal\Bear as MyTeddyBear;

new MyTeddyBear\Claw();
所以,从另一个包含用法的类继承另一个类,什么也不做,你必须重复你的用法,可能会简化它们,并且很可能不会为你实际上不在所说的类中使用的类添加用法(您知道好的IDE会提示您类中存在未使用的use语句,并帮助您将好的语句添加到
use
语句中吗?)


使用声明不像您相信的那样包含

它只是想说:“由于名称空间,我可以有多个同名的类,现在我想要使用的类实际上位于
use
定义的名称空间下。”

您不必在
use
语句中声明完整的命名空间

例如:

namespace App\Classes;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class MonthlyExcelReport extends ExcelReport
{
    public function __construct()
    {
       $reader = IOFactory::createReader('Xlsx');
       $workSheet = new Worksheet();
    }
}
可以这样缩短:

namespace App\Classes;

use PhpOffice\PhpSpreadsheet; // This means "all the classes that I am going to use, if not in the same namespace as the current class (App\Classes) would come from the namespace PhpOffice\PhpSpreadsheet"

class MonthlyExcelReport extends ExcelReport
{
    public function __construct()
    {
       $reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
       $workSheet = new PhpSpreadsheet\Worksheet\Worksheet();
    }
}
进一步阅读:

TL;DR; 名称空间允许您有两个名称相同但位于不同名称空间中的类

想象一下类
Animal\Bear\Claw
machine\Compactor\Claw
,在PHP引入名称空间之前,当我们需要像
Animal\u Bear\u Claw
machine\u Compactor\u Claw
这样丑陋的类名时,名称空间是可以实现的

现在,当您实例化或使用这些类时,您不想总是在扩展中说

new \Animal\Bear\Claw();
您希望能够说:“我在一个动物工厂模式的上下文中,基本上会作用于动物名称空间下的类,而不是机器的类”

所以你说:

use Animal\Bear\Claw;

new Claw();

甚至还有别名

use Animal\Bear as MyTeddyBear;

new MyTeddyBear\Claw();
所以,从另一个包含用法的类继承另一个类,什么也不做,你必须重复你的用法,可能会简化它们,并且很可能不会为你实际上不在所说的类中使用的类添加用法(您知道好的IDE会提示您类中存在未使用的use语句,并帮助您将好的语句添加到
use
语句中吗?)


使用声明不像您相信的那样包含

它只是想说:“由于名称空间,我可以有多个同名的类,现在我想要使用的类实际上位于
use
定义的名称空间下。”

您不必在
use
语句中声明完整的命名空间

例如:

namespace App\Classes;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class MonthlyExcelReport extends ExcelReport
{
    public function __construct()
    {
       $reader = IOFactory::createReader('Xlsx');
       $workSheet = new Worksheet();
    }
}
可以这样缩短:

namespace App\Classes;

use PhpOffice\PhpSpreadsheet; // This means "all the classes that I am going to use, if not in the same namespace as the current class (App\Classes) would come from the namespace PhpOffice\PhpSpreadsheet"

class MonthlyExcelReport extends ExcelReport
{
    public function __construct()
    {
       $reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
       $workSheet = new PhpSpreadsheet\Worksheet\Worksheet();
    }
}
进一步阅读:


那么基类中的“use”语句不会传播到扩展类中?那么基类中的“use”语句不会传播到扩展类中?