What is the difference between type and virtualType
In the di.xml
that comes with Magento2 there is a node type
and a node virtualType
. My questions is what is this virtualType
and in what case should it be used instead of type
?
In some places it looks like a symbolic link or rewrite:
<virtualType name="MagentoCoreModelSessionStorage" type="MagentoFrameworkSessionStorage">
Where one full path gets changed into another but in other places it appears to be used as a way to define a shorter alias.
<virtualType name="lessFileSourceBase" type="MagentoFrameworkViewFileCollectorBase">
magento2 dependency-injection virtualtype
add a comment |
In the di.xml
that comes with Magento2 there is a node type
and a node virtualType
. My questions is what is this virtualType
and in what case should it be used instead of type
?
In some places it looks like a symbolic link or rewrite:
<virtualType name="MagentoCoreModelSessionStorage" type="MagentoFrameworkSessionStorage">
Where one full path gets changed into another but in other places it appears to be used as a way to define a shorter alias.
<virtualType name="lessFileSourceBase" type="MagentoFrameworkViewFileCollectorBase">
magento2 dependency-injection virtualtype
2
I have no idea (yet) what they even mean but you can start digging from here:MagentoFrameworkObjectManagerConfigMapperDom::convert
. There is aswitch
statement in there somewhere.
– Marius♦
Aug 21 '14 at 14:34
Thanks @Marius, I am also wondering iflessFileSourceBase
is limited to the xml or if that can also be used outside. Guess I better get digging.
– David Manners
Aug 21 '14 at 14:36
add a comment |
In the di.xml
that comes with Magento2 there is a node type
and a node virtualType
. My questions is what is this virtualType
and in what case should it be used instead of type
?
In some places it looks like a symbolic link or rewrite:
<virtualType name="MagentoCoreModelSessionStorage" type="MagentoFrameworkSessionStorage">
Where one full path gets changed into another but in other places it appears to be used as a way to define a shorter alias.
<virtualType name="lessFileSourceBase" type="MagentoFrameworkViewFileCollectorBase">
magento2 dependency-injection virtualtype
In the di.xml
that comes with Magento2 there is a node type
and a node virtualType
. My questions is what is this virtualType
and in what case should it be used instead of type
?
In some places it looks like a symbolic link or rewrite:
<virtualType name="MagentoCoreModelSessionStorage" type="MagentoFrameworkSessionStorage">
Where one full path gets changed into another but in other places it appears to be used as a way to define a shorter alias.
<virtualType name="lessFileSourceBase" type="MagentoFrameworkViewFileCollectorBase">
magento2 dependency-injection virtualtype
magento2 dependency-injection virtualtype
edited Apr 20 '16 at 8:28
Fabian Schmengler
54.1k20127338
54.1k20127338
asked Aug 21 '14 at 14:28
David MannersDavid Manners
24.6k861211
24.6k861211
2
I have no idea (yet) what they even mean but you can start digging from here:MagentoFrameworkObjectManagerConfigMapperDom::convert
. There is aswitch
statement in there somewhere.
– Marius♦
Aug 21 '14 at 14:34
Thanks @Marius, I am also wondering iflessFileSourceBase
is limited to the xml or if that can also be used outside. Guess I better get digging.
– David Manners
Aug 21 '14 at 14:36
add a comment |
2
I have no idea (yet) what they even mean but you can start digging from here:MagentoFrameworkObjectManagerConfigMapperDom::convert
. There is aswitch
statement in there somewhere.
– Marius♦
Aug 21 '14 at 14:34
Thanks @Marius, I am also wondering iflessFileSourceBase
is limited to the xml or if that can also be used outside. Guess I better get digging.
– David Manners
Aug 21 '14 at 14:36
2
2
I have no idea (yet) what they even mean but you can start digging from here:
MagentoFrameworkObjectManagerConfigMapperDom::convert
. There is a switch
statement in there somewhere.– Marius♦
Aug 21 '14 at 14:34
I have no idea (yet) what they even mean but you can start digging from here:
MagentoFrameworkObjectManagerConfigMapperDom::convert
. There is a switch
statement in there somewhere.– Marius♦
Aug 21 '14 at 14:34
Thanks @Marius, I am also wondering if
lessFileSourceBase
is limited to the xml or if that can also be used outside. Guess I better get digging.– David Manners
Aug 21 '14 at 14:36
Thanks @Marius, I am also wondering if
lessFileSourceBase
is limited to the xml or if that can also be used outside. Guess I better get digging.– David Manners
Aug 21 '14 at 14:36
add a comment |
4 Answers
4
active
oldest
votes
Virtual types are a way to inject different dependencies into existing classes without affecting other classes.
For example, the MagentoFrameworkSessionStorage
class takes a $namespace
argument in its constructor, which defaults to the value 'default', and you could use the type
definition to change the namespace to 'core'.
<type name="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</type>
The above config would make it so that all instances of MagentoFrameworkSessionStorage
have a namespace of 'core'. Using a virtual type allows for the equivalent of a sub-class to be created, where only the sub-class has the altered argument values.
In the codebase we see the following two configurations:
<virtualType name="MagentoCoreModelSessionStorage" type="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</virtualType>
<type name="MagentoFrameworkSessionGeneric">
<arguments>
<argument name="storage" xsi:type="object">MagentoCoreModelSessionStorage</argument>
</arguments>
</type>
The first snippet creates a virtual type for MagentoCoreModelSessionStorage
which alters the namespace, and the second inject the virtual type into MagentoFrameworkSessionGeneric
. This allows MagentoFrameworkSessionGeneric
to be customized without affecting other classes that also declare a dependency on MagentoFrameworkSessionStorage
Thanks a lot @Chris finally some logical justification i found
– Suman-PHP4U
Aug 11 '17 at 12:11
That was simple and the best demonstration.
– Umar
Jan 1 at 14:56
add a comment |
Another way to understand virtual types -
Let's say that you have a class Class1
, which has the following constructor -
public function __construct(Class2 $argOfClass1){...}
And Class2
has the following constructor -
public function __construct(Class3 $argOfClass2){...}
Now, you want to change the type of $argOfClass2
from Class3
to Class4
, but only when Class2
is used as $argOfClass1
.
The "old" way to do that would be to add the following in di.xml
-
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
where Class5
is the following:
class Class5 extends Class2{
public function __construct(Class4 $argOfClass2){...}
}
Instead of using this way, you can use the virtual types to accomplish the same, by adding the following to di.xml
:
<virtualType name="Class5" type="Class2">
<arguments>
<argument name="argOfClass2" xsi:type="string">Class4</argument>
</arguments>
</virtualType>
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
As you can see, using the virtual type saved you the work of creation of Class5
.
For further reference I suggest to read Alan Storm's article regarding virtual types in Magento2 - http://alanstorm.com/magento_2_object_manager_virtual_types/
add a comment |
In the same di.xml
file I found that lessFileSourceBase
is passed as an argument for lessFileSourceBaseFiltered
that is passed as an argument for lessFileSourceBaseSorted
that is passed as an argument for type MagentoFrameworkLessFileCollectorAggregated
.
I found no other occurrence of lessFileSourceBase
(or lessFileSource
) in an other file except di.xml
from the core module. Only in some cache files but those are not important.
I guess if you are not going to use the virtual type in a PHP class, but only in the di
xml files then you are not required to make it look like a class name and you can use an alias.
But this is just pure speculation.
It will be "fun" to try to create a class and inject in its constructor an instance of lessFileSourceBase
to see how it behaves.
you missed the quotes around the word fun ;)
– David Manners
Aug 21 '14 at 15:06
1
@DavidManners. Right. I fixed it. :)
– Marius♦
Aug 21 '14 at 15:08
@Marius: If you alterMagentoFrameworkSessionGeneric
source file to depend onMagentoCoreModelSessionStorage
instead ofStorageInterface
you should get a 'Class MagentoCoreModelSessionStorage does not exist' exception. The reason being that ObjectManager doesn't create an instance of the virtualType, but just uses that to determine what arguments to provide for the constructor of the concrete type that is referenced by the virtualType definition (MagentoFrameworkSessionStorage
for the above example).
– Chris O'Toole
Aug 22 '14 at 19:43
Can see this in the Factory, where$requestedType
represents the virtual type and is used to gather arguments, but$type
is the concrete type that the virtualType maps to and is used for the object instantiation call.
– Chris O'Toole
Aug 22 '14 at 19:46
So even iflessFileSourceBase
was in a more namespaceclass type style, it wouldn't allow for direct reference by another php class, just for injection via the di.xml
– Chris O'Toole
Aug 22 '14 at 19:48
|
show 1 more comment
I wanted to throw-in my two cents to help anybody having difficulty understanding the difference between "type" and a "virtual type". A type definition tells Magento to map to a particular class when it runs into an instance of the target class. In any kind of dependency injection you need to pass a class(type) and the name of an object to the constructor or method. Keeping that in mind, a "Virtual Type" tells Magento: "No physical class exists for this injection, nor an object name given, So take my word for it and don't throw any errors. The Class and Name will be defined in this configuration." Then just like that.. Magento would create the class for you and save it in the 'generated' folder, which it would use to reference.
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f33103%2fwhat-is-the-difference-between-type-and-virtualtype%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Virtual types are a way to inject different dependencies into existing classes without affecting other classes.
For example, the MagentoFrameworkSessionStorage
class takes a $namespace
argument in its constructor, which defaults to the value 'default', and you could use the type
definition to change the namespace to 'core'.
<type name="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</type>
The above config would make it so that all instances of MagentoFrameworkSessionStorage
have a namespace of 'core'. Using a virtual type allows for the equivalent of a sub-class to be created, where only the sub-class has the altered argument values.
In the codebase we see the following two configurations:
<virtualType name="MagentoCoreModelSessionStorage" type="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</virtualType>
<type name="MagentoFrameworkSessionGeneric">
<arguments>
<argument name="storage" xsi:type="object">MagentoCoreModelSessionStorage</argument>
</arguments>
</type>
The first snippet creates a virtual type for MagentoCoreModelSessionStorage
which alters the namespace, and the second inject the virtual type into MagentoFrameworkSessionGeneric
. This allows MagentoFrameworkSessionGeneric
to be customized without affecting other classes that also declare a dependency on MagentoFrameworkSessionStorage
Thanks a lot @Chris finally some logical justification i found
– Suman-PHP4U
Aug 11 '17 at 12:11
That was simple and the best demonstration.
– Umar
Jan 1 at 14:56
add a comment |
Virtual types are a way to inject different dependencies into existing classes without affecting other classes.
For example, the MagentoFrameworkSessionStorage
class takes a $namespace
argument in its constructor, which defaults to the value 'default', and you could use the type
definition to change the namespace to 'core'.
<type name="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</type>
The above config would make it so that all instances of MagentoFrameworkSessionStorage
have a namespace of 'core'. Using a virtual type allows for the equivalent of a sub-class to be created, where only the sub-class has the altered argument values.
In the codebase we see the following two configurations:
<virtualType name="MagentoCoreModelSessionStorage" type="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</virtualType>
<type name="MagentoFrameworkSessionGeneric">
<arguments>
<argument name="storage" xsi:type="object">MagentoCoreModelSessionStorage</argument>
</arguments>
</type>
The first snippet creates a virtual type for MagentoCoreModelSessionStorage
which alters the namespace, and the second inject the virtual type into MagentoFrameworkSessionGeneric
. This allows MagentoFrameworkSessionGeneric
to be customized without affecting other classes that also declare a dependency on MagentoFrameworkSessionStorage
Thanks a lot @Chris finally some logical justification i found
– Suman-PHP4U
Aug 11 '17 at 12:11
That was simple and the best demonstration.
– Umar
Jan 1 at 14:56
add a comment |
Virtual types are a way to inject different dependencies into existing classes without affecting other classes.
For example, the MagentoFrameworkSessionStorage
class takes a $namespace
argument in its constructor, which defaults to the value 'default', and you could use the type
definition to change the namespace to 'core'.
<type name="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</type>
The above config would make it so that all instances of MagentoFrameworkSessionStorage
have a namespace of 'core'. Using a virtual type allows for the equivalent of a sub-class to be created, where only the sub-class has the altered argument values.
In the codebase we see the following two configurations:
<virtualType name="MagentoCoreModelSessionStorage" type="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</virtualType>
<type name="MagentoFrameworkSessionGeneric">
<arguments>
<argument name="storage" xsi:type="object">MagentoCoreModelSessionStorage</argument>
</arguments>
</type>
The first snippet creates a virtual type for MagentoCoreModelSessionStorage
which alters the namespace, and the second inject the virtual type into MagentoFrameworkSessionGeneric
. This allows MagentoFrameworkSessionGeneric
to be customized without affecting other classes that also declare a dependency on MagentoFrameworkSessionStorage
Virtual types are a way to inject different dependencies into existing classes without affecting other classes.
For example, the MagentoFrameworkSessionStorage
class takes a $namespace
argument in its constructor, which defaults to the value 'default', and you could use the type
definition to change the namespace to 'core'.
<type name="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</type>
The above config would make it so that all instances of MagentoFrameworkSessionStorage
have a namespace of 'core'. Using a virtual type allows for the equivalent of a sub-class to be created, where only the sub-class has the altered argument values.
In the codebase we see the following two configurations:
<virtualType name="MagentoCoreModelSessionStorage" type="MagentoFrameworkSessionStorage">
<arguments>
<argument name="namespace" xsi:type="string">core</argument>
</arguments>
</virtualType>
<type name="MagentoFrameworkSessionGeneric">
<arguments>
<argument name="storage" xsi:type="object">MagentoCoreModelSessionStorage</argument>
</arguments>
</type>
The first snippet creates a virtual type for MagentoCoreModelSessionStorage
which alters the namespace, and the second inject the virtual type into MagentoFrameworkSessionGeneric
. This allows MagentoFrameworkSessionGeneric
to be customized without affecting other classes that also declare a dependency on MagentoFrameworkSessionStorage
edited Jan 11 at 21:36
Rafael Corrêa Gomes
4,29222962
4,29222962
answered Aug 21 '14 at 21:50
Chris O'TooleChris O'Toole
3,19711515
3,19711515
Thanks a lot @Chris finally some logical justification i found
– Suman-PHP4U
Aug 11 '17 at 12:11
That was simple and the best demonstration.
– Umar
Jan 1 at 14:56
add a comment |
Thanks a lot @Chris finally some logical justification i found
– Suman-PHP4U
Aug 11 '17 at 12:11
That was simple and the best demonstration.
– Umar
Jan 1 at 14:56
Thanks a lot @Chris finally some logical justification i found
– Suman-PHP4U
Aug 11 '17 at 12:11
Thanks a lot @Chris finally some logical justification i found
– Suman-PHP4U
Aug 11 '17 at 12:11
That was simple and the best demonstration.
– Umar
Jan 1 at 14:56
That was simple and the best demonstration.
– Umar
Jan 1 at 14:56
add a comment |
Another way to understand virtual types -
Let's say that you have a class Class1
, which has the following constructor -
public function __construct(Class2 $argOfClass1){...}
And Class2
has the following constructor -
public function __construct(Class3 $argOfClass2){...}
Now, you want to change the type of $argOfClass2
from Class3
to Class4
, but only when Class2
is used as $argOfClass1
.
The "old" way to do that would be to add the following in di.xml
-
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
where Class5
is the following:
class Class5 extends Class2{
public function __construct(Class4 $argOfClass2){...}
}
Instead of using this way, you can use the virtual types to accomplish the same, by adding the following to di.xml
:
<virtualType name="Class5" type="Class2">
<arguments>
<argument name="argOfClass2" xsi:type="string">Class4</argument>
</arguments>
</virtualType>
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
As you can see, using the virtual type saved you the work of creation of Class5
.
For further reference I suggest to read Alan Storm's article regarding virtual types in Magento2 - http://alanstorm.com/magento_2_object_manager_virtual_types/
add a comment |
Another way to understand virtual types -
Let's say that you have a class Class1
, which has the following constructor -
public function __construct(Class2 $argOfClass1){...}
And Class2
has the following constructor -
public function __construct(Class3 $argOfClass2){...}
Now, you want to change the type of $argOfClass2
from Class3
to Class4
, but only when Class2
is used as $argOfClass1
.
The "old" way to do that would be to add the following in di.xml
-
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
where Class5
is the following:
class Class5 extends Class2{
public function __construct(Class4 $argOfClass2){...}
}
Instead of using this way, you can use the virtual types to accomplish the same, by adding the following to di.xml
:
<virtualType name="Class5" type="Class2">
<arguments>
<argument name="argOfClass2" xsi:type="string">Class4</argument>
</arguments>
</virtualType>
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
As you can see, using the virtual type saved you the work of creation of Class5
.
For further reference I suggest to read Alan Storm's article regarding virtual types in Magento2 - http://alanstorm.com/magento_2_object_manager_virtual_types/
add a comment |
Another way to understand virtual types -
Let's say that you have a class Class1
, which has the following constructor -
public function __construct(Class2 $argOfClass1){...}
And Class2
has the following constructor -
public function __construct(Class3 $argOfClass2){...}
Now, you want to change the type of $argOfClass2
from Class3
to Class4
, but only when Class2
is used as $argOfClass1
.
The "old" way to do that would be to add the following in di.xml
-
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
where Class5
is the following:
class Class5 extends Class2{
public function __construct(Class4 $argOfClass2){...}
}
Instead of using this way, you can use the virtual types to accomplish the same, by adding the following to di.xml
:
<virtualType name="Class5" type="Class2">
<arguments>
<argument name="argOfClass2" xsi:type="string">Class4</argument>
</arguments>
</virtualType>
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
As you can see, using the virtual type saved you the work of creation of Class5
.
For further reference I suggest to read Alan Storm's article regarding virtual types in Magento2 - http://alanstorm.com/magento_2_object_manager_virtual_types/
Another way to understand virtual types -
Let's say that you have a class Class1
, which has the following constructor -
public function __construct(Class2 $argOfClass1){...}
And Class2
has the following constructor -
public function __construct(Class3 $argOfClass2){...}
Now, you want to change the type of $argOfClass2
from Class3
to Class4
, but only when Class2
is used as $argOfClass1
.
The "old" way to do that would be to add the following in di.xml
-
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
where Class5
is the following:
class Class5 extends Class2{
public function __construct(Class4 $argOfClass2){...}
}
Instead of using this way, you can use the virtual types to accomplish the same, by adding the following to di.xml
:
<virtualType name="Class5" type="Class2">
<arguments>
<argument name="argOfClass2" xsi:type="string">Class4</argument>
</arguments>
</virtualType>
<type name="Class1">
<arguments>
<argument name="argOfClass1" xsi:type="object">Class5</argument>
</arguments>
</type>
As you can see, using the virtual type saved you the work of creation of Class5
.
For further reference I suggest to read Alan Storm's article regarding virtual types in Magento2 - http://alanstorm.com/magento_2_object_manager_virtual_types/
edited Apr 4 '17 at 14:40
7ochem
5,72293668
5,72293668
answered Apr 4 '17 at 14:22
NoamNNoamN
16316
16316
add a comment |
add a comment |
In the same di.xml
file I found that lessFileSourceBase
is passed as an argument for lessFileSourceBaseFiltered
that is passed as an argument for lessFileSourceBaseSorted
that is passed as an argument for type MagentoFrameworkLessFileCollectorAggregated
.
I found no other occurrence of lessFileSourceBase
(or lessFileSource
) in an other file except di.xml
from the core module. Only in some cache files but those are not important.
I guess if you are not going to use the virtual type in a PHP class, but only in the di
xml files then you are not required to make it look like a class name and you can use an alias.
But this is just pure speculation.
It will be "fun" to try to create a class and inject in its constructor an instance of lessFileSourceBase
to see how it behaves.
you missed the quotes around the word fun ;)
– David Manners
Aug 21 '14 at 15:06
1
@DavidManners. Right. I fixed it. :)
– Marius♦
Aug 21 '14 at 15:08
@Marius: If you alterMagentoFrameworkSessionGeneric
source file to depend onMagentoCoreModelSessionStorage
instead ofStorageInterface
you should get a 'Class MagentoCoreModelSessionStorage does not exist' exception. The reason being that ObjectManager doesn't create an instance of the virtualType, but just uses that to determine what arguments to provide for the constructor of the concrete type that is referenced by the virtualType definition (MagentoFrameworkSessionStorage
for the above example).
– Chris O'Toole
Aug 22 '14 at 19:43
Can see this in the Factory, where$requestedType
represents the virtual type and is used to gather arguments, but$type
is the concrete type that the virtualType maps to and is used for the object instantiation call.
– Chris O'Toole
Aug 22 '14 at 19:46
So even iflessFileSourceBase
was in a more namespaceclass type style, it wouldn't allow for direct reference by another php class, just for injection via the di.xml
– Chris O'Toole
Aug 22 '14 at 19:48
|
show 1 more comment
In the same di.xml
file I found that lessFileSourceBase
is passed as an argument for lessFileSourceBaseFiltered
that is passed as an argument for lessFileSourceBaseSorted
that is passed as an argument for type MagentoFrameworkLessFileCollectorAggregated
.
I found no other occurrence of lessFileSourceBase
(or lessFileSource
) in an other file except di.xml
from the core module. Only in some cache files but those are not important.
I guess if you are not going to use the virtual type in a PHP class, but only in the di
xml files then you are not required to make it look like a class name and you can use an alias.
But this is just pure speculation.
It will be "fun" to try to create a class and inject in its constructor an instance of lessFileSourceBase
to see how it behaves.
you missed the quotes around the word fun ;)
– David Manners
Aug 21 '14 at 15:06
1
@DavidManners. Right. I fixed it. :)
– Marius♦
Aug 21 '14 at 15:08
@Marius: If you alterMagentoFrameworkSessionGeneric
source file to depend onMagentoCoreModelSessionStorage
instead ofStorageInterface
you should get a 'Class MagentoCoreModelSessionStorage does not exist' exception. The reason being that ObjectManager doesn't create an instance of the virtualType, but just uses that to determine what arguments to provide for the constructor of the concrete type that is referenced by the virtualType definition (MagentoFrameworkSessionStorage
for the above example).
– Chris O'Toole
Aug 22 '14 at 19:43
Can see this in the Factory, where$requestedType
represents the virtual type and is used to gather arguments, but$type
is the concrete type that the virtualType maps to and is used for the object instantiation call.
– Chris O'Toole
Aug 22 '14 at 19:46
So even iflessFileSourceBase
was in a more namespaceclass type style, it wouldn't allow for direct reference by another php class, just for injection via the di.xml
– Chris O'Toole
Aug 22 '14 at 19:48
|
show 1 more comment
In the same di.xml
file I found that lessFileSourceBase
is passed as an argument for lessFileSourceBaseFiltered
that is passed as an argument for lessFileSourceBaseSorted
that is passed as an argument for type MagentoFrameworkLessFileCollectorAggregated
.
I found no other occurrence of lessFileSourceBase
(or lessFileSource
) in an other file except di.xml
from the core module. Only in some cache files but those are not important.
I guess if you are not going to use the virtual type in a PHP class, but only in the di
xml files then you are not required to make it look like a class name and you can use an alias.
But this is just pure speculation.
It will be "fun" to try to create a class and inject in its constructor an instance of lessFileSourceBase
to see how it behaves.
In the same di.xml
file I found that lessFileSourceBase
is passed as an argument for lessFileSourceBaseFiltered
that is passed as an argument for lessFileSourceBaseSorted
that is passed as an argument for type MagentoFrameworkLessFileCollectorAggregated
.
I found no other occurrence of lessFileSourceBase
(or lessFileSource
) in an other file except di.xml
from the core module. Only in some cache files but those are not important.
I guess if you are not going to use the virtual type in a PHP class, but only in the di
xml files then you are not required to make it look like a class name and you can use an alias.
But this is just pure speculation.
It will be "fun" to try to create a class and inject in its constructor an instance of lessFileSourceBase
to see how it behaves.
edited Aug 21 '14 at 15:07
answered Aug 21 '14 at 15:00
Marius♦Marius
164k28312663
164k28312663
you missed the quotes around the word fun ;)
– David Manners
Aug 21 '14 at 15:06
1
@DavidManners. Right. I fixed it. :)
– Marius♦
Aug 21 '14 at 15:08
@Marius: If you alterMagentoFrameworkSessionGeneric
source file to depend onMagentoCoreModelSessionStorage
instead ofStorageInterface
you should get a 'Class MagentoCoreModelSessionStorage does not exist' exception. The reason being that ObjectManager doesn't create an instance of the virtualType, but just uses that to determine what arguments to provide for the constructor of the concrete type that is referenced by the virtualType definition (MagentoFrameworkSessionStorage
for the above example).
– Chris O'Toole
Aug 22 '14 at 19:43
Can see this in the Factory, where$requestedType
represents the virtual type and is used to gather arguments, but$type
is the concrete type that the virtualType maps to and is used for the object instantiation call.
– Chris O'Toole
Aug 22 '14 at 19:46
So even iflessFileSourceBase
was in a more namespaceclass type style, it wouldn't allow for direct reference by another php class, just for injection via the di.xml
– Chris O'Toole
Aug 22 '14 at 19:48
|
show 1 more comment
you missed the quotes around the word fun ;)
– David Manners
Aug 21 '14 at 15:06
1
@DavidManners. Right. I fixed it. :)
– Marius♦
Aug 21 '14 at 15:08
@Marius: If you alterMagentoFrameworkSessionGeneric
source file to depend onMagentoCoreModelSessionStorage
instead ofStorageInterface
you should get a 'Class MagentoCoreModelSessionStorage does not exist' exception. The reason being that ObjectManager doesn't create an instance of the virtualType, but just uses that to determine what arguments to provide for the constructor of the concrete type that is referenced by the virtualType definition (MagentoFrameworkSessionStorage
for the above example).
– Chris O'Toole
Aug 22 '14 at 19:43
Can see this in the Factory, where$requestedType
represents the virtual type and is used to gather arguments, but$type
is the concrete type that the virtualType maps to and is used for the object instantiation call.
– Chris O'Toole
Aug 22 '14 at 19:46
So even iflessFileSourceBase
was in a more namespaceclass type style, it wouldn't allow for direct reference by another php class, just for injection via the di.xml
– Chris O'Toole
Aug 22 '14 at 19:48
you missed the quotes around the word fun ;)
– David Manners
Aug 21 '14 at 15:06
you missed the quotes around the word fun ;)
– David Manners
Aug 21 '14 at 15:06
1
1
@DavidManners. Right. I fixed it. :)
– Marius♦
Aug 21 '14 at 15:08
@DavidManners. Right. I fixed it. :)
– Marius♦
Aug 21 '14 at 15:08
@Marius: If you alter
MagentoFrameworkSessionGeneric
source file to depend on MagentoCoreModelSessionStorage
instead of StorageInterface
you should get a 'Class MagentoCoreModelSessionStorage does not exist' exception. The reason being that ObjectManager doesn't create an instance of the virtualType, but just uses that to determine what arguments to provide for the constructor of the concrete type that is referenced by the virtualType definition (MagentoFrameworkSessionStorage
for the above example).– Chris O'Toole
Aug 22 '14 at 19:43
@Marius: If you alter
MagentoFrameworkSessionGeneric
source file to depend on MagentoCoreModelSessionStorage
instead of StorageInterface
you should get a 'Class MagentoCoreModelSessionStorage does not exist' exception. The reason being that ObjectManager doesn't create an instance of the virtualType, but just uses that to determine what arguments to provide for the constructor of the concrete type that is referenced by the virtualType definition (MagentoFrameworkSessionStorage
for the above example).– Chris O'Toole
Aug 22 '14 at 19:43
Can see this in the Factory, where
$requestedType
represents the virtual type and is used to gather arguments, but $type
is the concrete type that the virtualType maps to and is used for the object instantiation call.– Chris O'Toole
Aug 22 '14 at 19:46
Can see this in the Factory, where
$requestedType
represents the virtual type and is used to gather arguments, but $type
is the concrete type that the virtualType maps to and is used for the object instantiation call.– Chris O'Toole
Aug 22 '14 at 19:46
So even if
lessFileSourceBase
was in a more namespaceclass type style, it wouldn't allow for direct reference by another php class, just for injection via the di.xml– Chris O'Toole
Aug 22 '14 at 19:48
So even if
lessFileSourceBase
was in a more namespaceclass type style, it wouldn't allow for direct reference by another php class, just for injection via the di.xml– Chris O'Toole
Aug 22 '14 at 19:48
|
show 1 more comment
I wanted to throw-in my two cents to help anybody having difficulty understanding the difference between "type" and a "virtual type". A type definition tells Magento to map to a particular class when it runs into an instance of the target class. In any kind of dependency injection you need to pass a class(type) and the name of an object to the constructor or method. Keeping that in mind, a "Virtual Type" tells Magento: "No physical class exists for this injection, nor an object name given, So take my word for it and don't throw any errors. The Class and Name will be defined in this configuration." Then just like that.. Magento would create the class for you and save it in the 'generated' folder, which it would use to reference.
New contributor
add a comment |
I wanted to throw-in my two cents to help anybody having difficulty understanding the difference between "type" and a "virtual type". A type definition tells Magento to map to a particular class when it runs into an instance of the target class. In any kind of dependency injection you need to pass a class(type) and the name of an object to the constructor or method. Keeping that in mind, a "Virtual Type" tells Magento: "No physical class exists for this injection, nor an object name given, So take my word for it and don't throw any errors. The Class and Name will be defined in this configuration." Then just like that.. Magento would create the class for you and save it in the 'generated' folder, which it would use to reference.
New contributor
add a comment |
I wanted to throw-in my two cents to help anybody having difficulty understanding the difference between "type" and a "virtual type". A type definition tells Magento to map to a particular class when it runs into an instance of the target class. In any kind of dependency injection you need to pass a class(type) and the name of an object to the constructor or method. Keeping that in mind, a "Virtual Type" tells Magento: "No physical class exists for this injection, nor an object name given, So take my word for it and don't throw any errors. The Class and Name will be defined in this configuration." Then just like that.. Magento would create the class for you and save it in the 'generated' folder, which it would use to reference.
New contributor
I wanted to throw-in my two cents to help anybody having difficulty understanding the difference between "type" and a "virtual type". A type definition tells Magento to map to a particular class when it runs into an instance of the target class. In any kind of dependency injection you need to pass a class(type) and the name of an object to the constructor or method. Keeping that in mind, a "Virtual Type" tells Magento: "No physical class exists for this injection, nor an object name given, So take my word for it and don't throw any errors. The Class and Name will be defined in this configuration." Then just like that.. Magento would create the class for you and save it in the 'generated' folder, which it would use to reference.
New contributor
edited 12 mins ago
New contributor
answered 42 mins ago
Timothy NongTimothy Nong
11
11
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f33103%2fwhat-is-the-difference-between-type-and-virtualtype%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I have no idea (yet) what they even mean but you can start digging from here:
MagentoFrameworkObjectManagerConfigMapperDom::convert
. There is aswitch
statement in there somewhere.– Marius♦
Aug 21 '14 at 14:34
Thanks @Marius, I am also wondering if
lessFileSourceBase
is limited to the xml or if that can also be used outside. Guess I better get digging.– David Manners
Aug 21 '14 at 14:36