Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Explaining the Factory Method Pattern
- abstract class Student
- {
- public $name;
- public $level;
- // The factory method doesn't know which type of certificates to issue
- // instead subclasses can define that type
- abstract public function issueCertificate();
- }
- abstract class Certificate
- {
- public $score;
- abstract public function calculateResults();
- }
- class ElementaryStudent extends Student
- {
- public function issueCertificate()
- {
- return new ElementaryCertificate();
- }
- }
- class PreparatoryStudent extends Student
- {
- public function issueCertificate()
- {
- return new PreparatoryCertificate();
- }
- }
- class ElementaryCertificate extends Certificate
- {
- public function calculateResults()
- {
- }
- }
- class PreparatoryCertificate extends Certificate
- {
- public function calculateResults()
- {
- }
- }
- $ahmed = new ElementaryStudent();
- $ahmed->name = 'Ahmed Mohammed Ibrahim';
- $ahmed->age = 5;
- $ahmedCertificate = $ahmed->issueCertificate();
- $ahmedCertificate->score = 100;
- var_dump($ahmedCertificate);
- $mohammed = new PreparatoryStudent();
- $mohammed->name = 'Mohammed Mohammed Ibrahim';
- $mohammed->age = 12;
- $mohammedCertificate = $mohammed->issueCertificate();
- $mohammedCertificate->score = 170;
- var_dump($mohammedCertificate);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement