Tôi có thể thiếu một điểm nhưng tôi chỉ tự hỏi tại sao đôi khi có một câu lệnh "sử dụng" cho một lớp cụ thể và đôi khi chúng ta không.
Ví dụ : app\code\Magento\Email\Model\Template.php, chúng tôi có ở đầu tệp:
namespace Magento\Email\Model;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
Sau đó, trong __constructphương thức chúng ta có các tham số sau:
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\View\DesignInterface $design,
\Magento\Framework\Registry $registry,
\Magento\Store\Model\App\Emulation $appEmulation,
StoreManagerInterface $storeManager,
\Magento\Framework\View\Asset\Repository $assetRepo,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Email\Model\Template\Config $emailConfig,
\Magento\Email\Model\TemplateFactory $templateFactory,
\Magento\Framework\Filter\FilterManager $filterManager,
\Magento\Framework\UrlInterface $urlModel,
\Magento\Email\Model\Template\FilterFactory $filterFactory,
array $data = []
)
Vì vậy, chúng ta có thể thấy rõ rằng như chúng ta đã gọi use Magento\Store\Model\StoreManagerInterface;ở đầu lớp, chúng ta có thể thực hiện StoreManagerInterface $storeManagertrong các tham số của hàm tạo.
Câu hỏi của tôi là:
- Tại sao chúng ta làm điều này chỉ cho một lớp?
- Tại sao chúng ta không thể thêm một
usecâu lệnh cho mỗi lớp của hàm tạo để chúng ta không phải nhập đường dẫn lớp đầy đủ? - Hoặc theo cách khác, tại sao chúng ta không loại bỏ
usetuyên bố và nhập đường dẫn đầy đủ đếnStoreManagerInterfacelớp?
usecho lớp cụ thể mà tôi đã chỉ đúng không?