Created
September 1, 2014 18:29
-
-
Save psaitu/6ea734dbb945b69f9fcc to your computer and use it in GitHub Desktop.
Php Factory Pattern Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Automobile | |
| { | |
| private $vehicle_make; | |
| private $vehicle_model; | |
| public function __construct($make, $model) | |
| { | |
| $this->vehicle_make = $make; | |
| $this->vehicle_model = $model; | |
| } | |
| public function get_make_and_model() | |
| { | |
| return $this->vehicle_make . ' ' . $this->vehicle_model; | |
| } | |
| } | |
| class AutomobileFactory | |
| { | |
| public static function create($make, $model) | |
| { | |
| return new Automobile($make, $model); | |
| } | |
| } | |
| // have the factory create the Automobile object | |
| $veyron = AutomobileFactory::create('Bugatti', 'Veyron'); | |
| print_r($veyron->get_make_and_model()); // outputs "Bugatti Veyron" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment