{"id":341459,"date":"2022-06-28T13:04:37","date_gmt":"2022-06-28T13:04:37","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=341459"},"modified":"2024-02-20T06:37:32","modified_gmt":"2024-02-20T06:37:32","slug":"how-getter-setter-automatically-generated-in-magento","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/","title":{"rendered":"How Getter Setter Automatically Generated in Magento?"},"content":{"rendered":"\n<p>Most of us do not create a getter or setter method for DB fields in model class but still while calling these methods why it doesn&#8217;t through any error?<\/p>\n\n\n\n<p>So in Magento, everything is used as an object and each model class somewhere extends \\Magento\\Framework\\DataObject::class in the parent. In this class __call method is actually responsible for the generation of getter and setter of any field (for e.g, setName($name) or getName()).<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/**\n     * Set\/Get attribute wrapper\n     *\n     * @param   string $method\n     * @param   array $args\n     * @return  mixed\n     * @throws \\Magento\\Framework\\Exception\\LocalizedException\n     *\/\n    public function __call($method, $args)\n    {\n        switch (substr($method, 0, 3)) {\n            case &#039;get&#039;:\n                $key = $this-&gt;_underscore(substr($method, 3));\n                $index = isset($args&#091;0]) ? $args&#091;0] : null;\n                return $this-&gt;getData($key, $index);\n            case &#039;set&#039;:\n                $key = $this-&gt;_underscore(substr($method, 3));\n                $value = isset($args&#091;0]) ? $args&#091;0] : null;\n                return $this-&gt;setData($key, $value);\n            case &#039;uns&#039;:\n                $key = $this-&gt;_underscore(substr($method, 3));\n                return $this-&gt;unsetData($key);\n            case &#039;has&#039;:\n                $key = $this-&gt;_underscore(substr($method, 3));\n                return isset($this-&gt;_data&#091;$key]);\n        }\n        throw new \\Magento\\Framework\\Exception\\LocalizedException(\n            new \\Magento\\Framework\\Phrase(&#039;Invalid method %1::%2&#039;, &#091;get_class($this), $method])\n        );\n    }<\/pre>\n\n\n\n<p>Let&#8217;s check its implementation.<\/p>\n\n\n\n<p>This __call() method is a magic method in PHP that executes when a call to an undefined method is made. So whenever we call any undefined method over a model object, the __call() methods checks for the get, set, uns, has prefixes in the called method name, then check for the field name and if all goes good we haven&#8217;t face any issue and we get the desired result.<\/p>\n\n\n\n<p>Thanks For Reading.<br>Happy Coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of us do not create a getter or setter method for DB fields in model class but still while calling these methods why it doesn&#8217;t through any error? So in Magento, everything is used as an object and each model class somewhere extends \\Magento\\Framework\\DataObject::class in the parent. In this class __call method is actually <a href=\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":376,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[12881,2070],"class_list":["post-341459","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-getter-setter","tag-magento2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How Getter Setter Automatically Generated in Magento? - Webkul Blog<\/title>\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\/how-getter-setter-automatically-generated-in-magento\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Getter Setter Automatically Generated in Magento? - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Most of us do not create a getter or setter method for DB fields in model class but still while calling these methods why it doesn&#8217;t through any error? So in Magento, everything is used as an object and each model class somewhere extends MagentoFrameworkDataObject::class in the parent. In this class __call method is actually [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/\" \/>\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-06-28T13:04:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-20T06:37:32+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=\"Deepak Tiwari\" \/>\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=\"Deepak Tiwari\" \/>\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\/how-getter-setter-automatically-generated-in-magento\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/\"},\"author\":{\"name\":\"Deepak Tiwari\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f1f005cfc0c9af7f93910041dce67985\"},\"headline\":\"How Getter Setter Automatically Generated in Magento?\",\"datePublished\":\"2022-06-28T13:04:37+00:00\",\"dateModified\":\"2024-02-20T06:37:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/\"},\"wordCount\":161,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"Getter Setter\",\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/\",\"name\":\"How Getter Setter Automatically Generated in Magento? - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2022-06-28T13:04:37+00:00\",\"dateModified\":\"2024-02-20T06:37:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How Getter Setter Automatically Generated in Magento?\"}]},{\"@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\/f1f005cfc0c9af7f93910041dce67985\",\"name\":\"Deepak Tiwari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d6f4542bf07455806d05215f870ee8d6ab4bfbdcd1446d3a3143de3b6b0680ba?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\/d6f4542bf07455806d05215f870ee8d6ab4bfbdcd1446d3a3143de3b6b0680ba?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Deepak Tiwari\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/deepak-tiwari301\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How Getter Setter Automatically Generated in Magento? - Webkul Blog","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\/how-getter-setter-automatically-generated-in-magento\/","og_locale":"en_US","og_type":"article","og_title":"How Getter Setter Automatically Generated in Magento? - Webkul Blog","og_description":"Most of us do not create a getter or setter method for DB fields in model class but still while calling these methods why it doesn&#8217;t through any error? So in Magento, everything is used as an object and each model class somewhere extends MagentoFrameworkDataObject::class in the parent. In this class __call method is actually [...]","og_url":"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2022-06-28T13:04:37+00:00","article_modified_time":"2024-02-20T06:37:32+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":"Deepak Tiwari","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Deepak Tiwari","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/"},"author":{"name":"Deepak Tiwari","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f1f005cfc0c9af7f93910041dce67985"},"headline":"How Getter Setter Automatically Generated in Magento?","datePublished":"2022-06-28T13:04:37+00:00","dateModified":"2024-02-20T06:37:32+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/"},"wordCount":161,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["Getter Setter","Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/","url":"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/","name":"How Getter Setter Automatically Generated in Magento? - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2022-06-28T13:04:37+00:00","dateModified":"2024-02-20T06:37:32+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-getter-setter-automatically-generated-in-magento\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How Getter Setter Automatically Generated in Magento?"}]},{"@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\/f1f005cfc0c9af7f93910041dce67985","name":"Deepak Tiwari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d6f4542bf07455806d05215f870ee8d6ab4bfbdcd1446d3a3143de3b6b0680ba?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\/d6f4542bf07455806d05215f870ee8d6ab4bfbdcd1446d3a3143de3b6b0680ba?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Deepak Tiwari"},"url":"https:\/\/webkul.com\/blog\/author\/deepak-tiwari301\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/341459","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\/376"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=341459"}],"version-history":[{"count":5,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/341459\/revisions"}],"predecessor-version":[{"id":422887,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/341459\/revisions\/422887"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=341459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=341459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=341459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}