Factory Method return you the specific object by params he gets.
Lets make an example… we got 2 different classes for students that extends StudentAbstract class,
now we can make factory class that return the class by param.
StudentAbstract.php
1 2 3 4 | abstract class StudentAbstract { abstract function getFirstName(); abstract function getLastName(); } |
FirstGradeStudent.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | include_once('StudentAbstract.php'); class FirstGradeStudent extends StudentAbstract { private $firstName; private $lastName; private function __construct() { $this->firstName = "Garry"; $this->lastName = "Lachman"; } public function getFirstName() { return $this->firstName; } public function getLastName() { return $this->lastName; } } |
SecondGradeStudent.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | include_once('StudentAbstract.php'); class SecondGradeStudent extends StudentAbstract { private $firstName; private $lastName; private function __construct() { $this->firstName = "Polani"; $this->lastName = "Almoni"; } public function getFirstName() { return $this->firstName; } public function getLastName() { return $this->lastName; } } |
GetStudentFactory.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | include_once("FirstGradeStudent.php"); include_once("SecondGradeStudent.php"); class GetStudentFactory { public function getStudent($grade) { $student = NULL; switch ($grade) { case "first": $student = new FirstGradeStudent(); break; case "second"; $student = new SecondGradeStudent(); break; default: $student = new FirstGradeStudent(); break; } return $student; } } |
Test.php
1 2 3 4 5 6 7 8 9 | include_once("GetStudentFactory.php"); $factory = new GetStudentFactory(); $firstGrade = $factory->getStudent("first"); // return FirstGradeStudent class $secondGrade = $factory->getStudent("second"); // return SecondGradeStudent class |
Have Fun ![]()
Garry Lachman
PHP MySQL Shopping Cart Tutorial in PHP
Very good & useful tutorial that i found while search the web about
Abstracting of shopping cart….
“Yes, this is a another shopping cart tutorial. I am planning to make this tutorial to cover a more sophisticated shopping cart solution but for now it only explains a basic shopping cart. I will improve it in time so stay tuned.”
Check if out here: http://www.phpwebcommerce.com/
Have fun,
Garry Lachman
UF4W22PSSX9C