3

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?

0

1 Answer 1

3

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.