{"id":685,"date":"2022-11-16T10:41:20","date_gmt":"2022-11-16T10:41:20","guid":{"rendered":"https:\/\/www.devopstrainer.in\/blog\/?p=685"},"modified":"2022-11-16T10:45:18","modified_gmt":"2022-11-16T10:45:18","slug":"what-is-this-keyword-in-php-use-cases-of-this-keyword","status":"publish","type":"post","link":"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/","title":{"rendered":"What is $this keyword in PHP? Use cases of $this keyword"},"content":{"rendered":"\n<p>Through this query, we will know about $this keyword in PHP and how can we use and apply it in PHP. So, first, we will define <strong><em> $this keyword<\/em><\/strong>. The $this keyword is an <strong>-&gt;<\/strong> operator applied on the class and object and it only points to the current class object. you remove the <strong>$<\/strong> sign in front of the property.<\/p>\n\n\n\n<p><strong>For ex:<\/strong><br>               $this ->color<br>               class Fruit<br>         {<br>            public $color;<br>            function showColor ($name)<br>     {<br>                 $this ->color = $name;<br>                echo &#8220;Color Name: $this ->color&#8221;;<br>       }<\/p>\n\n\n\n<p>                 $guava = new Fruit;<br>                $guava ->showColor(&#8216;green&#8217;);<\/p>\n\n\n\n<p>I hope you will understand the concept behind the $this keyword and if you will run this below-mentioned code in your php program, it will definitely run and show an accurate result according to the below-provided code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nclass Fruit{\n  var $color1;   \n  function showcolor(){\n    echo $this-&gt;color;\n  }\n}\n \n$guava = new Fruit;\n$guava-&gt;color=\"Green\";\n$guava-&gt;showColor();\n?&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Through this query, we will know about $this keyword in PHP and how can we use and apply it in PHP. So, first, we will define $this&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[263,264,262,113],"tags":[267,265,160,266],"class_list":["post-685","post","type-post","status-publish","format-standard","hentry","category-class","category-object","category-operator","category-php","tag-this-keyword","tag-this-keyword-in-php","tag-php-program","tag-use-cases-of-this-keyword"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is $this keyword in PHP? Use cases of $this keyword - DevOps | SRE | DevSecOps<\/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:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is $this keyword in PHP? Use cases of $this keyword - DevOps | SRE | DevSecOps\" \/>\n<meta property=\"og:description\" content=\"Through this query, we will know about $this keyword in PHP and how can we use and apply it in PHP. So, first, we will define $this...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps | SRE | DevSecOps\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-16T10:41:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-16T10:45:18+00:00\" \/>\n<meta name=\"author\" content=\"rahulkr kr\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rahulkr kr\" \/>\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:\\\/\\\/www.devopstrainer.in\\\/blog\\\/what-is-this-keyword-in-php-use-cases-of-this-keyword\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/what-is-this-keyword-in-php-use-cases-of-this-keyword\\\/\"},\"author\":{\"name\":\"rahulkr kr\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\"},\"headline\":\"What is $this keyword in PHP? Use cases of $this keyword\",\"datePublished\":\"2022-11-16T10:41:20+00:00\",\"dateModified\":\"2022-11-16T10:45:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/what-is-this-keyword-in-php-use-cases-of-this-keyword\\\/\"},\"wordCount\":137,\"commentCount\":0,\"keywords\":[\"$this keyword\",\"$this keyword in PHP\",\"PHP program\",\"Use cases of $this keyword\"],\"articleSection\":[\"Class\",\"Object\",\"Operator\",\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/what-is-this-keyword-in-php-use-cases-of-this-keyword\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/what-is-this-keyword-in-php-use-cases-of-this-keyword\\\/\",\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/what-is-this-keyword-in-php-use-cases-of-this-keyword\\\/\",\"name\":\"What is $this keyword in PHP? Use cases of $this keyword - DevOps | SRE | DevSecOps\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#website\"},\"datePublished\":\"2022-11-16T10:41:20+00:00\",\"dateModified\":\"2022-11-16T10:45:18+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/what-is-this-keyword-in-php-use-cases-of-this-keyword\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/what-is-this-keyword-in-php-use-cases-of-this-keyword\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/what-is-this-keyword-in-php-use-cases-of-this-keyword\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is $this keyword in PHP? Use cases of $this keyword\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/\",\"name\":\"DevOps | SRE | DevSecOps\",\"description\":\"Automation means Cost, Quality, Time\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\",\"name\":\"rahulkr kr\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g\",\"caption\":\"rahulkr kr\"},\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/author\\\/rahulkr\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is $this keyword in PHP? Use cases of $this keyword - DevOps | SRE | DevSecOps","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:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/","og_locale":"en_US","og_type":"article","og_title":"What is $this keyword in PHP? Use cases of $this keyword - DevOps | SRE | DevSecOps","og_description":"Through this query, we will know about $this keyword in PHP and how can we use and apply it in PHP. So, first, we will define $this...","og_url":"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/","og_site_name":"DevOps | SRE | DevSecOps","article_published_time":"2022-11-16T10:41:20+00:00","article_modified_time":"2022-11-16T10:45:18+00:00","author":"rahulkr kr","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rahulkr kr","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/#article","isPartOf":{"@id":"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/"},"author":{"name":"rahulkr kr","@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d"},"headline":"What is $this keyword in PHP? Use cases of $this keyword","datePublished":"2022-11-16T10:41:20+00:00","dateModified":"2022-11-16T10:45:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/"},"wordCount":137,"commentCount":0,"keywords":["$this keyword","$this keyword in PHP","PHP program","Use cases of $this keyword"],"articleSection":["Class","Object","Operator","PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/","url":"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/","name":"What is $this keyword in PHP? Use cases of $this keyword - DevOps | SRE | DevSecOps","isPartOf":{"@id":"https:\/\/www.devopstrainer.in\/blog\/#website"},"datePublished":"2022-11-16T10:41:20+00:00","dateModified":"2022-11-16T10:45:18+00:00","author":{"@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d"},"breadcrumb":{"@id":"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopstrainer.in\/blog\/what-is-this-keyword-in-php-use-cases-of-this-keyword\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopstrainer.in\/blog\/"},{"@type":"ListItem","position":2,"name":"What is $this keyword in PHP? Use cases of $this keyword"}]},{"@type":"WebSite","@id":"https:\/\/www.devopstrainer.in\/blog\/#website","url":"https:\/\/www.devopstrainer.in\/blog\/","name":"DevOps | SRE | DevSecOps","description":"Automation means Cost, Quality, Time","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopstrainer.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d","name":"rahulkr kr","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g","caption":"rahulkr kr"},"url":"https:\/\/www.devopstrainer.in\/blog\/author\/rahulkr\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/comments?post=685"}],"version-history":[{"count":3,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/685\/revisions"}],"predecessor-version":[{"id":689,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/685\/revisions\/689"}],"wp:attachment":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/media?parent=685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/categories?post=685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/tags?post=685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}