{"id":342362,"date":"2022-07-05T05:59:27","date_gmt":"2022-07-05T05:59:27","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=342362"},"modified":"2025-03-04T06:42:03","modified_gmt":"2025-03-04T06:42:03","slug":"create-a-preference-for-protected-type-methods","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/","title":{"rendered":"Create preference for protected methods in Magento 2."},"content":{"rendered":"\n<p>In this blog, we will discuss the <strong>preference for protected methods<\/strong> in Magento 2. But before that, let&#8217;s understand <a href=\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/\"><strong>Preferences<\/strong> in Magento 2<\/a>.<\/p>\n\n\n\n<p>Magento 2 provides several ways to modify code, such as <strong>Plugins, Observers, and Preferences<\/strong>. To use them effectively, we must understand their <strong>strengths and weaknesses<\/strong>.<\/p>\n\n\n\n<p>With <strong>Preference<\/strong>, the <strong>Object Manager<\/strong> replaces one class with another. It checks for a preference entry and swaps the original class with the preferred one when creating a new object.<\/p>\n\n\n\n<p>The main drawback is that this creates a <strong>parallel execution flow<\/strong>, preventing the original code from running. This can lead to <strong>logical errors and unexpected bugs<\/strong> in the system.<\/p>\n\n\n\n<p>Previously, Preferences were mainly used for <strong>public methods<\/strong>, but we can also create Preferences for <strong>protected methods<\/strong>. Let\u2019s explore how this works! \ud83d\ude80<\/p>\n\n\n\n<p>We need to map the preference file in the di.xml file.<\/p>\n\n\n\n<p>We have a Module named Demo Webkul_Demo.<\/p>\n\n\n\n<p>File Path:&nbsp;<code>app\/code\/Webkul\/Demo\/etc\/<\/code>di<code>.xml<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:ObjectManager\/etc\/config.xsd&quot;&gt;\n    &lt;preference     for=&quot;Webkul\\MobikulApi\\Rewrite\\Controller\\Catalog\\HomePageData&quot; type=&quot;Webkul\\Demo\\Rewrite\\Controller\\Catalog\\HomePageData&quot; \/&gt;\n&lt;\/config&gt;<\/pre>\n\n\n\n<p>Preference file path: <code>app\/code\/Webkul\/Demo<\/code>\/Rewrite\/Controller\/Catalog\/HomePageData.php<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Webkul\\Demo\\Rewrite\\Controller\\Catalog;\n\/**\n * HomePageData Preference Action Class\n *\/\nclass HomePageData extends \\Webkul\\MobikulApi\\Controller\\Catalog\\HomePageData\n{\n    protected function methodName()\n    {\n         # your override code.\n    }\n}<\/pre>\n\n\n\n<p>That&#8217;s all, now we can create the preference of public and protected both method types.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will discuss the preference for protected methods in Magento 2. But before that, let&#8217;s understand Preferences in Magento 2. Magento 2 provides several ways to modify code, such as Plugins, Observers, and Preferences. To use them effectively, we must understand their strengths and weaknesses. With Preference, the Object Manager replaces one <a href=\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":368,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[],"class_list":["post-342362","post","type-post","status-publish","format-standard","hentry","category-magento-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create preference for protected methods in Magento 2. - Webkul Blog<\/title>\n<meta name=\"description\" content=\"Create preference for protected methods in magento2\" \/>\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\/create-a-preference-for-protected-type-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create preference for protected methods in Magento 2. - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Create preference for protected methods in magento2\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/\" \/>\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=\"2022-07-05T05:59:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-04T06:42:03+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=\"Anurag Jain\" \/>\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=\"Anurag Jain\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/\"},\"author\":{\"name\":\"Anurag Jain\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/c62926ca988352d74ad5c2e85fcd4a63\"},\"headline\":\"Create preference for protected methods in Magento 2.\",\"datePublished\":\"2022-07-05T05:59:27+00:00\",\"dateModified\":\"2025-03-04T06:42:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/\"},\"wordCount\":181,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/\",\"url\":\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/\",\"name\":\"Create preference for protected methods in Magento 2. - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2022-07-05T05:59:27+00:00\",\"dateModified\":\"2025-03-04T06:42:03+00:00\",\"description\":\"Create preference for protected methods in magento2\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create preference for protected methods 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\/c62926ca988352d74ad5c2e85fcd4a63\",\"name\":\"Anurag Jain\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e15e522c60de7a76ffd1efc2152f6d848fcd5c14ce27b1d30bac29701d4c9837?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\/e15e522c60de7a76ffd1efc2152f6d848fcd5c14ce27b1d30bac29701d4c9837?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Anurag Jain\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/anuragjain-magento209\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create preference for protected methods in Magento 2. - Webkul Blog","description":"Create preference for protected methods in magento2","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\/create-a-preference-for-protected-type-methods\/","og_locale":"en_US","og_type":"article","og_title":"Create preference for protected methods in Magento 2. - Webkul Blog","og_description":"Create preference for protected methods in magento2","og_url":"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2022-07-05T05:59:27+00:00","article_modified_time":"2025-03-04T06:42:03+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":"Anurag Jain","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Anurag Jain","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/"},"author":{"name":"Anurag Jain","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/c62926ca988352d74ad5c2e85fcd4a63"},"headline":"Create preference for protected methods in Magento 2.","datePublished":"2022-07-05T05:59:27+00:00","dateModified":"2025-03-04T06:42:03+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/"},"wordCount":181,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/","url":"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/","name":"Create preference for protected methods in Magento 2. - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2022-07-05T05:59:27+00:00","dateModified":"2025-03-04T06:42:03+00:00","description":"Create preference for protected methods in magento2","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/create-a-preference-for-protected-type-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Create preference for protected methods 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\/c62926ca988352d74ad5c2e85fcd4a63","name":"Anurag Jain","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e15e522c60de7a76ffd1efc2152f6d848fcd5c14ce27b1d30bac29701d4c9837?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\/e15e522c60de7a76ffd1efc2152f6d848fcd5c14ce27b1d30bac29701d4c9837?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Anurag Jain"},"url":"https:\/\/webkul.com\/blog\/author\/anuragjain-magento209\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/342362","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\/368"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=342362"}],"version-history":[{"count":5,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/342362\/revisions"}],"predecessor-version":[{"id":484857,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/342362\/revisions\/484857"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=342362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=342362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=342362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}