{"id":550,"date":"2017-02-14T15:38:02","date_gmt":"2017-02-14T15:38:02","guid":{"rendered":"https:\/\/barkhane.com\/php\/2017\/02\/14\/classes-in-php\/"},"modified":"2024-03-03T14:58:43","modified_gmt":"2024-03-03T14:58:43","slug":"classes-in-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/classes-in-php\/","title":{"rendered":"Classes in PHP"},"content":{"rendered":"<p>Classes in PHP : A class is a unit of programmer-defined data type that describes the characteristics and behaviors of something, or of the group of things. A class includes local functions as well as local data<\/p>\n<p>For Example A class called Car, would describe the characteristics and behaviors common to all cars.<\/p>\n<p>A <a href=\"https:\/\/barkhane.com\/php\/\">PHP<\/a> Class has 3 main elements<\/p>\n<h3>PHP Data Member<\/h3>\n<p>Data members are also variable but difference is that they are in encapsulated form.<\/p>\n<h3>PHP Property<\/h3>\n<p>A property is a variable inside the class which can contain some information. Like Name is the property of student class.<\/p>\n<h3>PHP Method<\/h3>\n<p>Methods are functions inside the body of class, methods can also be accessible by those three types of users.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Syntax<\/strong> : To create a class, use PHP\u2019s <b>class<\/b> keyword.<\/p>\n<p><strong>Example<\/strong> :<\/p>\n<pre><code>  \nname = $name1; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/\/\/\/\/\/\/Note: Do not use $ symbol in front of properties.\n$this-&gt;age \u00a0= $age1;\n}\n\nPublic function get()\n{\necho $this-&gt;name;\u00a0\u00a0\u00a0\necho $this-&gt;age;\u00a0\u00a0\u00a0\n}\n\n}\n\u00a0\u00a0$obj =new Student(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/\/\/\/\/\/\/\/Creating Object\n\u00a0\u00a0$obj-&gt;set(\\\"bhopal\\\",60); \u00a0 \u00a0 \u00a0\/\/\/\/\/\/\/\/\/ Calling Class Function SET and passing value\u00a0\n\u00a0\u00a0echo $obj-&gt;get(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/\/\/\/\/\/\/\/ Calling Class Function\u00a0GET\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n?&gt;   <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Classes in PHP : A class is a unit of programmer-defined data type that describes the characteristics and behaviors of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-550","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=550"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/550\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}