{"id":383647,"date":"2023-05-26T04:41:27","date_gmt":"2023-05-26T04:41:27","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=383647"},"modified":"2023-05-26T06:49:27","modified_gmt":"2023-05-26T06:49:27","slug":"injectable-and-non-injectable-objects-in-magento2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/","title":{"rendered":"Injectable and Non-Injectable Objects in Magento 2"},"content":{"rendered":"\n<p>In this blog, we are going to learn very important concepts Injectable and Non-Injectable Objects in Magento 2.<\/p>\n\n\n\n<p><strong>What are Injectable Objects?<\/strong><\/p>\n\n\n\n<p>Injectable objects in Magento 2 are classes that can be instantiated and managed by the <a href=\"https:\/\/developer.adobe.com\/commerce\/php\/development\/build\/dependency-injection-file\/\" target=\"_blank\" rel=\"noreferrer noopener\">Dependency Injection (DI)<\/a> container. They are typically used for services, repositories, and other core functionalities.<\/p>\n\n\n\n<p>You can also check out our <a href=\"https:\/\/webkul.com\/blog\/dependency-injection\/\">Dependency Injection (DI)<\/a> blog for a better understanding of DI functionalities.<\/p>\n\n\n\n<p>Here&#8217;s an example of an injectable object in Magento 2:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">namespace Vendor\\Module\\Model;\n\nuse Vendor\\Module\\Api\\CustomInterface;\nuse Vendor\\Module\\Model\\Dependency;\n\nclass CustomModel implements CustomInterface\n{\n    protected $dependency;\n\n    public function __construct(Dependency $dependency)\n    {\n        $this-&gt;dependency = $dependency;\n    }\n\n    public function performAction()\n    {\n        \/\/ Use the dependency to perform the action\n        $this-&gt;dependency-&gt;doSomething();\n    }\n}<\/pre>\n\n\n\n<p>In the example above, the <strong>CustomModel<\/strong> class has a dependency on the <strong>Dependency<\/strong> class, which is injected through the constructor. By following this approach, we can easily manage and replace dependencies, making our code more flexible and maintainable.<\/p>\n\n\n\n<p><strong>What are Non-Injectable Objects?<\/strong><\/p>\n\n\n\n<p>Non-injectable objects in Magento 2 are those that cannot be automatically instantiated and managed by the DI container. These objects are typically instantiated using the <code><strong>new<\/strong><\/code> keyword directly in the code. Non-injectable objects may still have dependencies, but their instantiation is not controlled by the DI container.<\/p>\n\n\n\n<p>Let&#8217;s consider an example of a non-injectable object:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">namespace Vendor\\Module\\Model;\n\nuse Vendor\\Module\\Api\\CustomInterface;\nuse Vendor\\Module\\Model\\Dependency;\n\nclass CustomModel implements CustomInterface\n{\n    public function performAction()\n    {\n        $dependency = new Dependency();\n        \/\/ Use the dependency to perform the action\n        $dependency-&gt;doSomething();\n    }\n}<\/pre>\n\n\n\n<p>In the above example, the <code><strong>CustomModel<\/strong><\/code> class instantiates the <code><strong>Dependency<\/strong><\/code> object directly using the <code><strong>new<\/strong><\/code> keyword. This approach tightly couples the classes together, making it difficult to replace dependencies or write unit tests.<\/p>\n\n\n\n<p><strong>When to Use Injectable and Non-Injectable Objects?<\/strong><\/p>\n\n\n\n<p>It&#8217;s recommended to use injectable objects whenever possible, as they promote flexibility, maintainability, and testability. Injectable objects allow for easy swapping of dependencies and facilitate writing unit tests by enabling the use of mock objects.<\/p>\n\n\n\n<p>Hope this will be helpful.<br><\/p>\n\n\n\n<p>Thanks \ud83d\ude42<\/p>\n\n\n\n<p>You can see other blogs as well:-<\/p>\n\n\n\n<p><a href=\"https:\/\/webkul.com\/blog\/add-custom-product-attributes-in-magento-2\/\">Add Custom Product Attributes<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/webkul.com\/blog\/add-a-custom-customer-group-in-magento2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Add Custom Customer Group<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/webkul.com\/blog\/get-dropdown-options-dependent-dropdown-admin-ui-component\/\">Get Dropdown Options From Dependent Dropdown<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we are going to learn very important concepts Injectable and Non-Injectable Objects in Magento 2. What are Injectable Objects? Injectable objects in Magento 2 are classes that can be instantiated and managed by the Dependency Injection (DI) container. They are typically used for services, repositories, and other core functionalities. You can also <a href=\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":354,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302],"tags":[14271,14117,2070,14272],"class_list":["post-383647","post","type-post","status-publish","format-standard","hentry","category-magento2","tag-injectable","tag-injectable-and-non-injectable-objects","tag-magento2","tag-non-injectable"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Injectable and Non-Injectable Objects in Magento 2 - Webkul Blog<\/title>\n<meta name=\"description\" content=\"Injectable objects can be instantiated using constructor injection. Non-injectable objects cannot be automatically instantiated.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Injectable and Non-Injectable Objects in Magento 2 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Injectable objects can be instantiated using constructor injection. Non-injectable objects cannot be automatically instantiated.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-26T04:41:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-26T06:49:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mubarik\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mubarik\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/\"},\"author\":{\"name\":\"Mubarik\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/2fa581764e96125af674349150b2b818\"},\"headline\":\"Injectable and Non-Injectable Objects in Magento 2\",\"datePublished\":\"2023-05-26T04:41:27+00:00\",\"dateModified\":\"2023-05-26T06:49:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/\"},\"wordCount\":277,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"Injectable\",\"Injectable and Non-injectable Objects\",\"Magento2\",\"Non-Injectable\"],\"articleSection\":[\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/\",\"url\":\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/\",\"name\":\"Injectable and Non-Injectable Objects in Magento 2 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2023-05-26T04:41:27+00:00\",\"dateModified\":\"2023-05-26T06:49:27+00:00\",\"description\":\"Injectable objects can be instantiated using constructor injection. Non-injectable objects cannot be automatically instantiated.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Injectable and Non-Injectable Objects in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/2fa581764e96125af674349150b2b818\",\"name\":\"Mubarik\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/af8c0b8b43a042ae4d9fd3cb5207b18bbe898d7ab63344df28ac22803b26a0ee?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/af8c0b8b43a042ae4d9fd3cb5207b18bbe898d7ab63344df28ac22803b26a0ee?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Mubarik\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/md-mubarik459\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Injectable and Non-Injectable Objects in Magento 2 - Webkul Blog","description":"Injectable objects can be instantiated using constructor injection. Non-injectable objects cannot be automatically instantiated.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/","og_locale":"en_US","og_type":"article","og_title":"Injectable and Non-Injectable Objects in Magento 2 - Webkul Blog","og_description":"Injectable objects can be instantiated using constructor injection. Non-injectable objects cannot be automatically instantiated.","og_url":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2023-05-26T04:41:27+00:00","article_modified_time":"2023-05-26T06:49:27+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Mubarik","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Mubarik","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/"},"author":{"name":"Mubarik","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/2fa581764e96125af674349150b2b818"},"headline":"Injectable and Non-Injectable Objects in Magento 2","datePublished":"2023-05-26T04:41:27+00:00","dateModified":"2023-05-26T06:49:27+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/"},"wordCount":277,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["Injectable","Injectable and Non-injectable Objects","Magento2","Non-Injectable"],"articleSection":["Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/","url":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/","name":"Injectable and Non-Injectable Objects in Magento 2 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2023-05-26T04:41:27+00:00","dateModified":"2023-05-26T06:49:27+00:00","description":"Injectable objects can be instantiated using constructor injection. Non-injectable objects cannot be automatically instantiated.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/injectable-and-non-injectable-objects-in-magento2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Injectable and Non-Injectable Objects in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/2fa581764e96125af674349150b2b818","name":"Mubarik","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/af8c0b8b43a042ae4d9fd3cb5207b18bbe898d7ab63344df28ac22803b26a0ee?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/af8c0b8b43a042ae4d9fd3cb5207b18bbe898d7ab63344df28ac22803b26a0ee?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Mubarik"},"url":"https:\/\/webkul.com\/blog\/author\/md-mubarik459\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/383647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/354"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=383647"}],"version-history":[{"count":8,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/383647\/revisions"}],"predecessor-version":[{"id":383711,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/383647\/revisions\/383711"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=383647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=383647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=383647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}