Skip to content

Instantly share code, notes, and snippets.

@erikwiffin
Created June 10, 2015 02:08
Show Gist options
  • Save erikwiffin/4ba755e8534253e322c2 to your computer and use it in GitHub Desktop.
Save erikwiffin/4ba755e8534253e322c2 to your computer and use it in GitHub Desktop.
<?php
class Address
{
public function __construct(
$street1,
$street2,
$city,
$state,
$zip,
$country
) {
$this->street1 = $street1;
$this->street2 = $stree2;
$this->city = $city;
$this->state = $state;
$this->zip = $zip;
$this->country = $county;
}
}
class AddressBuilder
{
protected $street1 = null;
protected $street2 = null;
protected $city = null;
protected $state = null;
protected $zip = null;
protected $country = null;
public function setStreet1($value)
{
$this->street1 = $value;
return $this;
}
public function setStreet2($value)
{
$this->street2 = $value;
return $this;
}
public function setCity($value)
{
$this->city = $value;
return $this;
}
public function setState($value)
{
$this->state = $value;
return $this;
}
public function setZip($value)
{
$this->zip = $value;
return $this;
}
public function setCountry($value)
{
$this->country = $value;
return $this;
}
public function build()
{
return new Address(
$this->street1,
$this->street2,
$this->city,
$this->state,
$this->zip,
$this->country
);
}
}
// What does this even?
$address1 = new Address('123 Main St.', null, null, null, 27701, null);
// Obviously
$builder = new AddressBuilder();
$address2 = $builder
->setStreet1('123 Main St.')
->setZip(27701)
->build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment