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:

  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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top