{"id":790,"date":"2018-11-14T15:07:33","date_gmt":"2018-11-14T15:07:33","guid":{"rendered":"https:\/\/barkhane.com\/php\/2018\/11\/14\/object-in-php\/"},"modified":"2024-03-03T14:57:28","modified_gmt":"2024-03-03T14:57:28","slug":"object-in-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/object-in-php\/","title":{"rendered":"Object in PHP"},"content":{"rendered":"<header class=\"entry-header\">\n<p class=\"entry-title\">Object in PHP<\/p>\n<\/header>\n<div class=\"entry-content\">\n<p>An\u00a0<strong>Object<\/strong>\u00a0is an individual instance of the data structure defined by a <a href=\"https:\/\/barkhane.com\/php\/classes-in-php\/\">class<\/a>. We define a class once and then make many objects that belong to it. Objects are also known as instances.<\/p>\n<p><strong>Creating an Object:<\/strong><br \/>\nFollowing is an example of how to create object using\u00a0<strong>new<\/strong>\u00a0operator.<\/p>\n<pre>class Book {\n   \/\/ Members of class Book\n}\n\n\/\/ Creating three objects of book\n$physics = new Books;\n$maths = new Books;\n$chemistry = new Books;\n<\/pre>\n<p><strong>Member Functions:<\/strong><br \/>\nAfter creating our objects, we can call member functions related to that object. A member function typically accesses members of current object only.<\/p>\n<p>Example:<\/p>\n<pre>$physics-&gt;setTitle( \\\"Physics for High School\\\" );\n$chemistry-&gt;setTitle( \\\"Advanced Chemistry\\\" );\n$maths-&gt;setTitle( \\\"Algebra\\\" );\n\n$physics-&gt;setPrice( 10 );\n$chemistry-&gt;setPrice( 15 );\n$maths-&gt;setPrice( 7 );<\/pre>\n<p>The following syntax used are for the following program elaborated in the example given below:<\/p>\n<p>Example:<\/p>\n<div id=\"highlighter_704711\" class=\"syntaxhighlighter nogutter \">\n<pre>&lt;?php\n\nclass Books {\n\n\/* Member variables *\/\nvar $price;\nvar $title;\n\n\/* Member functions *\/\nfunction setPrice($par){\n$this-&gt;price = $par;\n}\n\nfunction getPrice(){\necho $this-&gt;price.\\\"&lt;br&gt;\\\";\n}\n\nfunction setTitle($par){\n$this-&gt;title = $par;\n}\n\nfunction getTitle(){\necho $this-&gt;title.\\\"&lt;br&gt;\\\" ;\n}\n}\n\n\/* Creating New object using \\\"new\\\" operator *\/\n$maths = new Books;\n\n\/* Setting title and prices for the object *\/\n$maths-&gt;setTitle( \\\"Algebra\\\" );\n$maths-&gt;setPrice( 7 );\n\n\/* Calling Member Functions *\/\n$maths-&gt;getTitle();\n$maths-&gt;getPrice();\n?&gt;<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Object in PHP An\u00a0Object\u00a0is an individual instance of the data structure defined by a class. We define a class once [&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-790","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/790","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=790"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/790\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}