What is the difference between use Magento\… under namespace and __construct(Magento\…)? [duplicate]

magento.stackexchange https://magento.stackexchange.com/questions/186758

Domanda

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:

  1. Using use Magento\Customer\Model\Customer under namespace

    namespace Namespace\Module\Setup;
    
    use Magento\Customer\Model\Customer;
    
    class Abc implements Xyz
    {
        protected $customer;
    
        public function __construct(
            Customer $customer
        ) {
            $this->customer = $customer;
        }
        ...
    }
    
  2. 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?

È stato utile?

Soluzione

The best practice if you want follow PHP Standards Recommendations (http://www.php-fig.org/psr/) is use your first point.

  1. 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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top