Difference between declare class in : â__constructâ and in the top of file with âuseâ [duplicate]
Clash 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;
magento2 class construct
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.
add a comment |Â
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;
magento2 class construct
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
add a comment |Â
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;
magento2 class construct
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
magento2 class construct
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
add a comment |Â
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
add a comment |Â
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 :)
add a comment |Â
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.
add a comment |Â
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 :)
add a comment |Â
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 :)
add a comment |Â
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 :)
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 :)
answered Sep 7 at 7:32
Marjan
6567
6567
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Sep 7 at 7:31
Sukumar Gorai
4,6653424
4,6653424
add a comment |Â
add a comment |Â
Thanks prince..
â Niranjan Gondaliya
Sep 7 at 8:52