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







magento2 class construct






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 7 at 8:36









Prince

6,4862934




6,4862934










asked Sep 7 at 7:13









Niranjan Gondaliya

765




765




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



























    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
























        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






















          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












          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












          share|improve this answer



          share|improve this answer










          answered Sep 7 at 7:32









          Marjan

          6567




          6567






















              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,6653424




                  4,6653424












                      這個網誌中的熱門文章

                      How to combine Bézier curves to a surface?

                      Carbon dioxide

                      Why am i infinitely getting the same tweet with the Twitter Search API?