{"id":5964,"date":"2022-07-29T04:56:38","date_gmt":"2022-07-29T04:56:38","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=5964"},"modified":"2022-07-29T04:56:38","modified_gmt":"2022-07-29T04:56:38","slug":"builder-pattern-in-php","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/","title":{"rendered":"Builder Pattern in PHP"},"content":{"rendered":"<p>The builder design pattern is a creative design pattern that can be used in cases when we need to create special class objects that are flexible and complex with the possibilities of dynamically setting properties and objects.<\/p>\n<p>In this pattern, a director and a builder work together to build an object. The director controls the building and indicates what components and variations will go into an object. The builder knows how to assemble the specification given.<\/p>\n<h2>Structure:<\/h2>\n<ul>\n<li>Concrete class<\/li>\n<li>Builder Interface<\/li>\n<li>Different Builder Implementations<\/li>\n<li>Director<\/li>\n<li>Client code<\/li>\n<\/ul>\n<p><strong><em>Lets see how builder will look like with some simple code examples:<\/em><\/strong><\/p>\n<ul>\n<li>Concrete class(Vehicle)<br \/>\n<code>&lt;?php<br \/>\n\/\/ Concrete Class<br \/>\nclass Vehicle<br \/>\n{<br \/>\n   public $model;<br \/>\n   public $engines_count;<br \/>\n   public $type;<br \/>\n   const CAR = \"Car\";<br \/>\n   const BUS = \"Bus\";<br \/>\n   public function __construct(){<br \/>\n   }<br \/>\n}<\/code>\n<\/li>\n<li>Now, we will create the builder interface named \u201cVehicleBuilderInterface\u201d to set each component of the vehicle object creation. The method getVehicle will return the final object.<br \/>\n<code>\/\/ Builder interface<br \/>\ninterface VehicleBuilderInterface<br \/>\n{<br \/>\n   public function setVehicleModel();<br \/>\n   public function setEnginesCount();<br \/>\n   public function setVehicleType();<br \/>\n   public function getVehicle();<br \/>\n}<\/code>\n<\/li>\n<li>Now, we will implement the different builders by extending the Builder interface. As you see,  We are passing the original concrete class(Vehicle) in every constructor. Then we are calling the helper functions such as <strong>setModel<\/strong>, and <strong>setType <\/strong>to set the properties of the object. In the end,  we called <strong>getVehicle <\/strong>to return the Vehicle object.<br \/>\n<code>class HondaCityCarBuilder implements VehicleBuilderInterface<br \/>\n{<br \/>\n   private $vehicle;<br \/>\n   public function __construct(Vehicle $vehicle)<br \/>\n   {<br \/>\n       $this->vehicle = $vehicle;<br \/>\n   }<br \/>\n   public function setVehicleModel()<br \/>\n   {<br \/>\n       $this->vehicle->model = \"Honda City\";<br \/>\n   }<br \/>\n   public function setEnginesCount()<br \/>\n   {<br \/>\n       $this->vehicle->enginesCount = 1;<br \/>\n   }<br \/>\n   public function setVehicleType()<br \/>\n   {<br \/>\n       $this->vehicle->type = Vehicle::CAR;<br \/>\n   }<br \/>\n   public function getVehicle()<br \/>\n   {<br \/>\n       return $this->vehicle;<br \/>\n   }<br \/>\n}<br \/>\nclass MahindraBusBuilder implements VehicleBuilderInterface<br \/>\n{<br \/>\n   private $vehicle;<br \/>\n   public function __construct(Vehicle $vehicle)<br \/>\n   {<br \/>\n       $this->vehicle = $vehicle;<br \/>\n   }<br \/>\n   public function setVehicleModel()<br \/>\n   {<br \/>\n       $this->vehicle->model = \"Mahindra\";<br \/>\n   }<br \/>\n   public function setEnginesCount()<br \/>\n   {<br \/>\n       $this->vehicle->enginesCount = 2;<br \/>\n   }<br \/>\n   public function setVehicleType()<br \/>\n   {<br \/>\n       $this->vehicle->type = Vehicle::BUS;<br \/>\n   }<br \/>\n   public function getVehicle()<br \/>\n   {<br \/>\n       return $this->vehicle;<br \/>\n   }<br \/>\n}<\/code>\n<\/li>\n<li>A director class is for creating an object using the builder interface. It is only responsible for running the building steps in a specific order. The director&#8217;s main responsibility is to get a builder interface and call the builder&#8217;s methods and then retrieve the object.<br \/>\n<code>class VehicleDirector<br \/>\n{<br \/>\n   public function build(VehicleBuilderInterface $builder)<br \/>\n   {<br \/>\n       $builder->setVehicleModel();<br \/>\n       $builder->setEnginesCount();<br \/>\n       $builder->setVehicleType();<br \/>\n       return $builder->getVehicle();<br \/>\n   }<br \/>\n}<\/code>\n<\/li>\n<li>Client code: The client code creates a builder object, and sends it to the director. Then the director starts the construction process. The final result is obtained from the builder object.<br \/>\n<code>$hondaCityCar = (new VehicleDirector())->build(new HondaCityCarBuilder(new Vehicle()));<br \/>\n$mahindraBus = (new VehicleDirector())->build(new MahindraBusBuilder(new Vehicle()));<br \/>\nOutput:<br \/>\necho \"&lt;pre>\";<br \/>\nvar_dump($hondaCityCar);<br \/>\necho \"\\n\";<br \/>\necho \"Vehicle Model: \" . $hondaCityCar->model . \"\\n\";<br \/>\necho \"Vehicle Engines: \" . $hondaCityCar->engines_count . \"\\n\";<br \/>\necho \"Vehicle Type: \" . $hondaCityCar->type . \"\\n\";<\/code>\n<\/li>\n<\/ul>\n<p><small><em>jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The <a href=\"https:\/\/www.studysection.com\/jquery-3.x-advanced\">jQuery Certification exam<\/a> by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The builder design pattern is a creative design pattern that can be used in cases when we need to create<\/p>\n","protected":false},"author":1,"featured_media":5965,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[758,200],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Builder Pattern in PHP - StudySection Blog<\/title>\n<meta name=\"description\" content=\"The builder pattern is a creative design pattern that can be used in cases when we need to create special class objects\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Builder Pattern in PHP - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"The builder pattern is a creative design pattern that can be used in cases when we need to create special class objects\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-29T04:56:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/07\/Builder-Pattern-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Builder Pattern in PHP\",\"datePublished\":\"2022-07-29T04:56:38+00:00\",\"dateModified\":\"2022-07-29T04:56:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/\"},\"wordCount\":308,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Pattern\",\"php\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/\",\"url\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/\",\"name\":\"Builder Pattern in PHP - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-07-29T04:56:38+00:00\",\"dateModified\":\"2022-07-29T04:56:38+00:00\",\"description\":\"The builder pattern is a creative design pattern that can be used in cases when we need to create special class objects\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Builder Pattern in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Builder Pattern in PHP - StudySection Blog","description":"The builder pattern is a creative design pattern that can be used in cases when we need to create special class objects","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:\/\/studysection.com\/blog\/builder-pattern-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Builder Pattern in PHP - StudySection Blog","og_description":"The builder pattern is a creative design pattern that can be used in cases when we need to create special class objects","og_url":"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-07-29T04:56:38+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/07\/Builder-Pattern-1.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Builder Pattern in PHP","datePublished":"2022-07-29T04:56:38+00:00","dateModified":"2022-07-29T04:56:38+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/"},"wordCount":308,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Pattern","php"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/builder-pattern-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/","url":"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/","name":"Builder Pattern in PHP - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-07-29T04:56:38+00:00","dateModified":"2022-07-29T04:56:38+00:00","description":"The builder pattern is a creative design pattern that can be used in cases when we need to create special class objects","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/builder-pattern-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/builder-pattern-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Builder Pattern in PHP"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":287,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5964"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=5964"}],"version-history":[{"count":2,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5964\/revisions"}],"predecessor-version":[{"id":5967,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5964\/revisions\/5967"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/5965"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=5964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=5964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=5964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}