PHP Factory Pattern Step By Step Tutorial - Part 2: We have learned creating factory within the object itself at previous post. In this post, we will look how to implement factory pattern with external class. Please look following code:
<?
class Vehicle{
function category($wheel = 0){
if($wheel == 2){
return "Motor";
}elseif($wheel == 4){
return "Car";
}
}
}
class GetVehicle{
function get($wheel){
return Vehicle::category($wheel);
}
}
class Spec{
var $wheel = '';
var $vc = '';
function Spec($wheel){
$this->wheel = $wheel;
$this->vc =& GetVehicle::get($wheel);
}
function name(){
return $this->vc;
}
function brand(){
if($this->vc == "Motor"){
return array('Harley','Honda');
}elseif($this->vc = "Car"){
return array('Nisan','Opel');
}
}
}
$kind = new Spec(2);
echo "Kind: ".$kind->name();
echo "<br>";
echo "Brand: ".implode(",",$kind->brand());
?>