Last active
April 15, 2016 00:29
-
-
Save AysadKozanoglu/73039beba0f0f538741e61f259770709 to your computer and use it in GitHub Desktop.
This code uses a factory to create the Bike object. There are two possible benefits to building your code this way; the first is that if you need to change, rename, or replace the Bike class later on you can do so and you will only have to modify the code in the factory, instead of every place in your project that uses the Bike class. The second…
This file contains 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 | |
#//////////////////////////////////////////////////////////////////// | |
# | |
# | |
# written by: Aysad Kozanoglu | |
# email: k.a@tuta.io | |
# website: http://onweb.pe.hu | |
# | |
# MIT licence | |
# https://en.wikipedia.org/wiki/MIT_License | |
# | |
# more information about factory patterns | |
# -> https://en.wikipedia.org/wiki/Factory_%28object-oriented_programming%29 | |
# | |
# example usage: | |
# $bike = BikeFactory::create('mercedes', 'ebike-100'); | |
# echo $bike->getMakeAndModel(); // outputs "mercedes ebike-100" | |
# | |
class bike | |
{ | |
private $vehicleMake; | |
private $vehicleModel; | |
public function __construct($make, $model) | |
{ | |
$this->vehicleMake = $make; | |
$this->vehicleModel = $model; | |
} | |
public function getMakeAndModel() | |
{ | |
return $this->vehicleMake . ' ' . $this->vehicleModel; | |
} | |
} | |
class BikeFactory | |
{ | |
public static function create($make, $model) | |
{ | |
return new bike($make, $model); | |
} | |
} | |
// have the factory create the bike object | |
$bike = BikeFactory::create('mercedes', 'ebike-100'); | |
print_r($bike->getMakeAndModel()); // outputs "mercedes ebike-100" | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment