In Magento 2 there are a set of classes that are generated automatically and stored in var/generation and they are used in the core code.
While trying to create a custom module by replicating one of the core modules I saw that also for my module there are some generated classes.
In the generated classes I've seen proxys, interceptors, factories....

Is there a complete list of classes or type of classes that are auto generated?
And as side question...when/where are they generated?


There are different reasons to generate different entities:

Proxy - it's used for lazy-loading. But each type requires separate proxy class so that would be annoying for developers to write them manually so Magento generates them. Yo will only find references to Proxies in di.xml

Factory - is a class that is used to create non-injectables (entities). Same as proxies. PHP doesn't have generics yet, so we auto-generate factories not to make developer write boilerplate code. You will interact with factories across Magento code. Most probably you will require your own factories for your entities. Just ask for corresponding factory in constructor and it will get generated on the next run or during compiler run.

Interceptor - is a infrastructure class, that is used by Interception component. As Flyingmana and Phil Winkle noticed you should not interact with them anyhow.

There are a bunch of other entities generated (like Logger for object graph profiling mode) but these 3 are most important.

Magento 2 can also be compiled. Compilation makes DI and Interception faster. All code generation and compilation can be done with dev/tools/Magento/Tools/Di/compiler.php

| improve this answer | |

From the Concept:

You dont need to know which type of classes get auto generated, as you never have to touch or use them , not even you should.

They get (still?) generated on the fly, but should later get generated via cli command or similar.

The list which types exist could grow any time, when the internal parts get the need for a new one. But access to them would always happen hidden behind the normal documented APIs ( commonly known as function calls )

| improve this answer | |
  • 1
    I'm not sure about "or use them". Here is an example: In Magento\Cms\Block\Block in the constructor an instance of \Magento\Cms\Model\BlockFactory is used. That class is nowhere in the code. It is found only in var/generation. So if I'm doing a similar module I might have to use a similar class. – Marius Sep 16 '14 at 15:46

This is how Magento generates the aspect-oriented approach to the new platform; by actually generating the before, around, and after "events" instead of dynamically dispatching them. This strikes a balance of convenience to the programmer and performance for the platform.

Some techniques such as the auto dependency injection are flattened into the generated classes rather than executed via reflection - again - this is a technique to improve performance.

| improve this answer | |
  • Thanks for explaining the "why?", but my main questions were "what?" and "where?". – Marius Sep 17 '14 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.