Both are most important topics for learning Magento2. I want to discuss what learn from google, pdf,notes,magento documentation etc.
Magento 2 provides a new approach to working with objects. It not only encourages you to use the object manager, but also creates an object for you. Unlike Magento 1, you no longer register class groups in the configuration as there are no more special factory names. Now, you need to use the real class name -- including the PHP namespace -- when you require an instance of a class.
The creation of objects is such an important process that Magento 2 handles it automatically via the object manager and class dependencies declared as constructor parameters. The object manager creates the objects you need injected into your classes.
In Magento 2, the object manager is responsible for creating all objects a class requires. You define your class with the constructor receiving the argument objects that it needs.
The dependency injection system -- basically the object manager -- will create them for you. All this is based on configuration.
Dependency injection is configuration that is XML based, and validated by XSD. Magento 2 uses dependency injection as an alternative to the Magento 1.x Mage class.
The following object manager classes are located under Magento\Framework:
ObjectManagerInterface, which provides the interface implemented by the ObjectManager.
ObjectManager\ObjectManager and App\ObjectManager, which both
provide an implementation (App\ObjectManager extending
ObjectManagerFactory, the class that creates the object manager.These two factories used by the object manager to create instances of classes.
App\ObjectManager\Factory\Dynamic\Developer (In developer Mode) App\ObjectManager\Factory\Dynamic\Production (In Production Mode)
Example: Assume you need a storeManager instance in your Product class. You can declare an argument with the type StoreManagerInterface in the constructor of the Product. Now, using di.xml, you have to define which class will be substituted for that interface.
public function __construct( ……. \Magento\Store\Model\StoreManagerInterface $storeManager, ……... )
app/etc : di.xml
<preference for="Magento\Store\Model\StoreManagerInterface" type="Magento\Store\Model\StoreManager" />
For that, you have two options:
Define a preference and it will work globally (which means that every other class that declares an argument with the type StoreManagerInterface will receive what you defined as the preference).
Assign a class in your di.xml for this particular object (Product), and the preference will not be global, but only apply to Product.