Dependency injection :
Magento 2 uses dependency injection.In simple terms, when object A requires object or value B to fulfill a function, then B is a dependency of A Or A is dependent on B. Basically it avoids creating object manually.
Constructor injection :
The di.xml file configures which dependencies to inject by the object manager. Each module can have a global and area-specific di.xml file. Magento reads all the di.xml configuration files declared in the system and merges them all together by appending all nodes. Pass parameter to constructor using di.xml
The object manager is a Magento service class that helps instantiate objects at the beginning of the bootstrapping process.
Initiate object manager
Magento 2 uses dependency injection.In simple terms, when object A requires object or value B to fulfill a function, then B is a dependency of A Or A is dependent on B. Basically it avoids creating object manually.
Constructor injection :
public function __construct(NameSpace\ModuleName\Model $modelFactory) {
$this->_modelFactory = $modelFactory;
}
Method injection :
public function processCommand(\NameSpace\ModuleName\Model $model)
{
// processCommand Code
}
di.xml : The di.xml file configures which dependencies to inject by the object manager. Each module can have a global and area-specific di.xml file. Magento reads all the di.xml configuration files declared in the system and merges them all together by appending all nodes. Pass parameter to constructor using di.xml
<type name="path_to_class_file">
<arguments>
<argument name="mainTable" xsi:type="string">table_name</argument>
<argument name="resourceModel" xsi:type="string">Namespace\ModuleName\Model\ResourceModel\BlogPosts</argument>
</arguments>
</type>
Now mainTable and resourceModel can use in class constructorpublic function __construct(
$mainTable,
$resourceModel) {}
Override Class Block Model of Core using di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="CORE_CLASS_PATH" type="NAMESPACE_CLASS_PATH" />
</config>
Object manager : The object manager is a Magento service class that helps instantiate objects at the beginning of the bootstrapping process.
Initiate object manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
Comments
Post a Comment