Difference between declare class in : “__construct” and in the top of file with “use” [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite













This question already has an answer here:



  • Magento 2: use statement versus direct class path?

    2 answers



  • Magento Setting Up Cron “cron.sh: line 26: $'r': command not found”

    1 answer



Anyone have idea about declare class in __construct and declare class in top of file with use.



Exemple:



use MagentoStoreModelStoreManagerInterface;


and



public function __construct(
MagentoStoreModelStoreManagerInterface $storeManager,
)
$this->_storeManager = $storeManager;







share|improve this question














marked as duplicate by Prince, Manashvi Birla, Dhiren Vasoya, Jai, Priyank Sep 8 at 4:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Thanks prince..
    – Niranjan Gondaliya
    Sep 7 at 8:52
















up vote
2
down vote

favorite













This question already has an answer here:



  • Magento 2: use statement versus direct class path?

    2 answers



  • Magento Setting Up Cron “cron.sh: line 26: $'r': command not found”

    1 answer



Anyone have idea about declare class in __construct and declare class in top of file with use.



Exemple:



use MagentoStoreModelStoreManagerInterface;


and



public function __construct(
MagentoStoreModelStoreManagerInterface $storeManager,
)
$this->_storeManager = $storeManager;







share|improve this question














marked as duplicate by Prince, Manashvi Birla, Dhiren Vasoya, Jai, Priyank Sep 8 at 4:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Thanks prince..
    – Niranjan Gondaliya
    Sep 7 at 8:52












up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:



  • Magento 2: use statement versus direct class path?

    2 answers



  • Magento Setting Up Cron “cron.sh: line 26: $'r': command not found”

    1 answer



Anyone have idea about declare class in __construct and declare class in top of file with use.



Exemple:



use MagentoStoreModelStoreManagerInterface;


and



public function __construct(
MagentoStoreModelStoreManagerInterface $storeManager,
)
$this->_storeManager = $storeManager;







share|improve this question















This question already has an answer here:



  • Magento 2: use statement versus direct class path?

    2 answers



  • Magento Setting Up Cron “cron.sh: line 26: $'r': command not found”

    1 answer



Anyone have idea about declare class in __construct and declare class in top of file with use.



Exemple:



use MagentoStoreModelStoreManagerInterface;


and



public function __construct(
MagentoStoreModelStoreManagerInterface $storeManager,
)
$this->_storeManager = $storeManager;





This question already has an answer here:



  • Magento 2: use statement versus direct class path?

    2 answers



  • Magento Setting Up Cron “cron.sh: line 26: $'r': command not found”

    1 answer









share|improve this question













share|improve this question




share|improve this question








edited Sep 7 at 8:36









Prince

6,2262932




6,2262932










asked Sep 7 at 7:13









Niranjan Gondaliya

515




515




marked as duplicate by Prince, Manashvi Birla, Dhiren Vasoya, Jai, Priyank Sep 8 at 4:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Prince, Manashvi Birla, Dhiren Vasoya, Jai, Priyank Sep 8 at 4:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Thanks prince..
    – Niranjan Gondaliya
    Sep 7 at 8:52
















  • Thanks prince..
    – Niranjan Gondaliya
    Sep 7 at 8:52















Thanks prince..
– Niranjan Gondaliya
Sep 7 at 8:52




Thanks prince..
– Niranjan Gondaliya
Sep 7 at 8:52










2 Answers
2






active

oldest

votes

















up vote
3
down vote













Passing the full name space directly in the constructor is straightforward and easy.



All in one place, you do not have to scroll up to figure out what has just arrived at your constructor.



The use statement has one nice feature that allows aliases like



use MagentoStoreModelStoreManagerInterface as TheStorageInterface


and your refer to this type by its alias like



public function __construct(
TheStorageInterface $theStorage,
)
$this->_storeManager = $theStorage;



Yes, you could type just about anything, but usually this is the name of the class/interface.



One thing to notice: if you need to inject the StoreManagerInterfaceonce only once per class, just like magento usually does with constructor injection, it really does not make much difference.



But if you would need to inject twice or more times, for example in two different methods, than maybe the alias usage is better.



Eventually, there is no right or wrong. It is just a matter of taste, or, you simply have no choice but to follow what has been established as a practice in a given project.



And no, I would never give such a name for the alias (TheStorageInterface) in a real program :)






share|improve this answer








New contributor




Marjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote













    If you define the class like below in the top of the page:



    use MagentoStoreModelStoreManagerInterface;


    then you __contruct function will be look like this:



    public function __construct(
    StoreManagerInterface $storeManager,
    )
    $this->_storeManager = $storeManager;



    and if you not define the class in top then you can also managed it by injecting directly in your __construct function like below:



    public function __construct(
    MagentoStoreModelStoreManagerInterface $storeManager,
    )
    $this->_storeManager = $storeManager;



    Both are same.






    share|improve this answer



























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      Passing the full name space directly in the constructor is straightforward and easy.



      All in one place, you do not have to scroll up to figure out what has just arrived at your constructor.



      The use statement has one nice feature that allows aliases like



      use MagentoStoreModelStoreManagerInterface as TheStorageInterface


      and your refer to this type by its alias like



      public function __construct(
      TheStorageInterface $theStorage,
      )
      $this->_storeManager = $theStorage;



      Yes, you could type just about anything, but usually this is the name of the class/interface.



      One thing to notice: if you need to inject the StoreManagerInterfaceonce only once per class, just like magento usually does with constructor injection, it really does not make much difference.



      But if you would need to inject twice or more times, for example in two different methods, than maybe the alias usage is better.



      Eventually, there is no right or wrong. It is just a matter of taste, or, you simply have no choice but to follow what has been established as a practice in a given project.



      And no, I would never give such a name for the alias (TheStorageInterface) in a real program :)






      share|improve this answer








      New contributor




      Marjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





















        up vote
        3
        down vote













        Passing the full name space directly in the constructor is straightforward and easy.



        All in one place, you do not have to scroll up to figure out what has just arrived at your constructor.



        The use statement has one nice feature that allows aliases like



        use MagentoStoreModelStoreManagerInterface as TheStorageInterface


        and your refer to this type by its alias like



        public function __construct(
        TheStorageInterface $theStorage,
        )
        $this->_storeManager = $theStorage;



        Yes, you could type just about anything, but usually this is the name of the class/interface.



        One thing to notice: if you need to inject the StoreManagerInterfaceonce only once per class, just like magento usually does with constructor injection, it really does not make much difference.



        But if you would need to inject twice or more times, for example in two different methods, than maybe the alias usage is better.



        Eventually, there is no right or wrong. It is just a matter of taste, or, you simply have no choice but to follow what has been established as a practice in a given project.



        And no, I would never give such a name for the alias (TheStorageInterface) in a real program :)






        share|improve this answer








        New contributor




        Marjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



















          up vote
          3
          down vote










          up vote
          3
          down vote









          Passing the full name space directly in the constructor is straightforward and easy.



          All in one place, you do not have to scroll up to figure out what has just arrived at your constructor.



          The use statement has one nice feature that allows aliases like



          use MagentoStoreModelStoreManagerInterface as TheStorageInterface


          and your refer to this type by its alias like



          public function __construct(
          TheStorageInterface $theStorage,
          )
          $this->_storeManager = $theStorage;



          Yes, you could type just about anything, but usually this is the name of the class/interface.



          One thing to notice: if you need to inject the StoreManagerInterfaceonce only once per class, just like magento usually does with constructor injection, it really does not make much difference.



          But if you would need to inject twice or more times, for example in two different methods, than maybe the alias usage is better.



          Eventually, there is no right or wrong. It is just a matter of taste, or, you simply have no choice but to follow what has been established as a practice in a given project.



          And no, I would never give such a name for the alias (TheStorageInterface) in a real program :)






          share|improve this answer








          New contributor




          Marjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          Passing the full name space directly in the constructor is straightforward and easy.



          All in one place, you do not have to scroll up to figure out what has just arrived at your constructor.



          The use statement has one nice feature that allows aliases like



          use MagentoStoreModelStoreManagerInterface as TheStorageInterface


          and your refer to this type by its alias like



          public function __construct(
          TheStorageInterface $theStorage,
          )
          $this->_storeManager = $theStorage;



          Yes, you could type just about anything, but usually this is the name of the class/interface.



          One thing to notice: if you need to inject the StoreManagerInterfaceonce only once per class, just like magento usually does with constructor injection, it really does not make much difference.



          But if you would need to inject twice or more times, for example in two different methods, than maybe the alias usage is better.



          Eventually, there is no right or wrong. It is just a matter of taste, or, you simply have no choice but to follow what has been established as a practice in a given project.



          And no, I would never give such a name for the alias (TheStorageInterface) in a real program :)







          share|improve this answer








          New contributor




          Marjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Marjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Sep 7 at 7:32









          Marjan

          3516




          3516




          New contributor




          Marjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Marjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Marjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






















              up vote
              0
              down vote













              If you define the class like below in the top of the page:



              use MagentoStoreModelStoreManagerInterface;


              then you __contruct function will be look like this:



              public function __construct(
              StoreManagerInterface $storeManager,
              )
              $this->_storeManager = $storeManager;



              and if you not define the class in top then you can also managed it by injecting directly in your __construct function like below:



              public function __construct(
              MagentoStoreModelStoreManagerInterface $storeManager,
              )
              $this->_storeManager = $storeManager;



              Both are same.






              share|improve this answer
























                up vote
                0
                down vote













                If you define the class like below in the top of the page:



                use MagentoStoreModelStoreManagerInterface;


                then you __contruct function will be look like this:



                public function __construct(
                StoreManagerInterface $storeManager,
                )
                $this->_storeManager = $storeManager;



                and if you not define the class in top then you can also managed it by injecting directly in your __construct function like below:



                public function __construct(
                MagentoStoreModelStoreManagerInterface $storeManager,
                )
                $this->_storeManager = $storeManager;



                Both are same.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  If you define the class like below in the top of the page:



                  use MagentoStoreModelStoreManagerInterface;


                  then you __contruct function will be look like this:



                  public function __construct(
                  StoreManagerInterface $storeManager,
                  )
                  $this->_storeManager = $storeManager;



                  and if you not define the class in top then you can also managed it by injecting directly in your __construct function like below:



                  public function __construct(
                  MagentoStoreModelStoreManagerInterface $storeManager,
                  )
                  $this->_storeManager = $storeManager;



                  Both are same.






                  share|improve this answer












                  If you define the class like below in the top of the page:



                  use MagentoStoreModelStoreManagerInterface;


                  then you __contruct function will be look like this:



                  public function __construct(
                  StoreManagerInterface $storeManager,
                  )
                  $this->_storeManager = $storeManager;



                  and if you not define the class in top then you can also managed it by injecting directly in your __construct function like below:



                  public function __construct(
                  MagentoStoreModelStoreManagerInterface $storeManager,
                  )
                  $this->_storeManager = $storeManager;



                  Both are same.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 7 at 7:31









                  Sukumar Gorai

                  4,0962424




                  4,0962424












                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery