What is the difference between type and virtualType












32















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">









share|improve this question




















  • 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











  • 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
















32















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">









share|improve this question




















  • 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











  • 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














32












32








32


16






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">









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 20 '16 at 8:28









Fabian Schmengler

54.1k20127337




54.1k20127337










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














  • 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











  • 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








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










3 Answers
3






active

oldest

votes


















56














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






share|improve this answer


























  • 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



















14














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/






share|improve this answer

































    8














    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.






    share|improve this answer


























    • 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 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











    • 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











    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
    });


    }
    });














    draft saved

    draft discarded


















    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

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    56














    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






    share|improve this answer


























    • 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
















    56














    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






    share|improve this answer


























    • 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














    56












    56








    56







    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






    share|improve this answer















    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 11 at 21:36









    Rafael Corrêa Gomes

    4,28222962




    4,28222962










    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



















    • 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













    14














    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/






    share|improve this answer






























      14














      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/






      share|improve this answer




























        14












        14








        14







        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/






        share|improve this answer















        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/







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 4 '17 at 14:40









        7ochem

        5,72193668




        5,72193668










        answered Apr 4 '17 at 14:22









        NoamNNoamN

        16316




        16316























            8














            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.






            share|improve this answer


























            • 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 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











            • 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
















            8














            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.






            share|improve this answer


























            • 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 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











            • 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














            8












            8








            8







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 21 '14 at 15:07

























            answered Aug 21 '14 at 15:00









            MariusMarius

            164k28312662




            164k28312662













            • 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 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











            • 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



















            • 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 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











            • 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

















            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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            William S. Burroughs

            Eda skans

            1924