Factory Pattern implementation in PHP5
Factory Pattern
The factory pattern is a class that contains the methods which create objects at run time.
Implementation
Following code implemented factory pattern:
class DB{
public static function getDB($className){
$className = $type.”_”.”db”;
return new $className;
}
}
$db = DB::getDB(’MySQL’);
- The above code is creating object for which database class we need to use. For example If we change argument in MYSQL to something. Then object for that class will be created.
