{"id":810,"date":"2018-11-15T03:32:55","date_gmt":"2018-11-15T03:32:55","guid":{"rendered":"https:\/\/barkhane.com\/php\/2018\/11\/15\/constructor-functions-in-php\/"},"modified":"2024-03-03T14:54:52","modified_gmt":"2024-03-03T14:54:52","slug":"constructor-functions-in-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/constructor-functions-in-php\/","title":{"rendered":"Constructor Functions in PHP"},"content":{"rendered":"<p>Constructor Functions in PHP are special type of functions which are called automatically whenever an <a href=\"https:\/\/barkhane.com\/php\/object-in-php\/\">object<\/a> is created. So we take full advantage of this behaviour, by initializing many things through constructor functions.<\/p>\n<p>PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.<\/p>\n<p>Following example will create one constructor for Books class and it will initialize price and title for the book at the time of object creation.<\/p>\n<pre><code>\nfunction __construct( $par1, $par2 ) {\n$this-&gt;title = $par1;\n$this-&gt;price = $par2;\n}\n\n<\/code><\/pre>\n<p>Now we don\\&#8217;t need to call set function separately to set price and title. We can initialize these two member variables at the time of object creation only. Check following example below \u2212<\/p>\n<pre><code>\n$physics = new Books( \\\"Physics for High School\\\", 10 );\n$maths = new Books ( \\\"Advanced Chemistry\\\", 15 );\n$chemistry = new Books (\\\"Algebra\\\", 7 );\n\n\/* Get those set values *\/\n$physics-&gt;getTitle();\n$chemistry-&gt;getTitle();\n$maths-&gt;getTitle();\n\n$physics-&gt;getPrice();\n$chemistry-&gt;getPrice();\n$maths-&gt;getPrice();\n\n<\/code><\/pre>\n<p>This will produce the following result \u2212<\/p>\n<p>Physics for High School<br \/>\nAdvanced Chemistry<br \/>\nAlgebra<br \/>\n10<br \/>\n15<br \/>\n7<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Constructor Functions in PHP are special type of functions which are called automatically whenever an object is created. So we [&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-810","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/810","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=810"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/810\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}