What is the difference between use Magento\… under namespace and __construct(Magento\…)? [duplicate]
-
11-10-2020 - |
Question
As I'm writing a new module, this question just raise out my curiousity.
Let's take Magento\Customer\Model\Customer
as an example, the below codes can perform the same function actually:
Using
use Magento\Customer\Model\Customer
undernamespace
namespace Namespace\Module\Setup; use Magento\Customer\Model\Customer; class Abc implements Xyz { protected $customer; public function __construct( Customer $customer ) { $this->customer = $customer; } ... }
Directly declare inside
__construct
namespace Namespace\Module\Setup; class Abc implements Xyz { protected $customer; public function __construct( Magento\Customer\Model\Customer $customer ) { $this->customer = $customer; } ... }
My question is: What's the difference between them in terms of backend operations? And which one is more compliance to the best practice?
Solution
The best practice if you want follow PHP Standards Recommendations (http://www.php-fig.org/psr/) is use your first point.
- Using use Magento\Customer\Model\Customer under namespace
In that link, you can look all the information about this topic. You must read the PSRs with status accepted, especially the PSR1, PSR2 and PSR4.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange