Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Object Oriented Programming in PHP

Introduction to Object Oriented Programming in PHP

Powiemy sobie o podstawowych strukturach, elementach języka oraz wzorcach związanych z programowaniem obiektowym w PHP. Zahaczymy o polimorfizm, abstrakcję, hermetyzację oraz o zasady SOLID

Leszek Prabucki

July 27, 2015
Tweet

More Decks by Leszek Prabucki

Other Decks in Programming

Transcript

  1. <?php namespace Cocoders\MedicalClinic; class Clinic { /** * @var Employee[]

    */ private $employees = []; public function hireEmployee(Employee $employee) { $this->employees[$employee->getTaxNumber()] = $employee; } } @cocoders
  2. class Clinic { /** * @var PatientCase[] */ private $patientCases

    = []; //.. public function registerPatient(Patient $patient, $hasInsurance) { if ($case = $this->findPatientCase($patient->getIdNumber())) { $case->registerVisit($patient, $hasInsurance); return; } $this->patientCases[$patient->getIdNumber()] = new PatientCase($patient, $hasInsurance); } } @cocoders
  3. @cocoders Alan Kay – creator of Smalltalk (1998) Smalltalk is

    not only NOT its syntax or the class library, it is not even about classes. The big idea is "messaging" - that is what the kernel of Smalltalk/Squeak is all about
  4. @cocoders namespace Cocoders\MedicalClinic; abstract class Employee { private $taxNumber; public

    function __construct($taxNumber) { $this->taxNumber = $taxNumber; } public function getTaxNumber() { return $this->taxNumber; } }
  5. @cocoders namespace Cocoders\MedicalClinic\Employee; use Cocoders\MedicalClinic\Employee; class Doctor extends Employee {

    private $licenseNumber; private $specializations = []; public function __construct($taxNumber, $licenseNumber, $specializaitons) { parent::__construct($taxNumber); $this->licenseNumber = $licenseNumber; $this->specializations = $specializaitons; } public function hasSpecialization($specialization) { return false !== array_search($specialization, $this->specializations, true); } }
  6. @cocoders <?php namespace Cocoders\MedicalClinic; interface ClinicRegistry { /** * @param

    $id * @return Clinic */ public function find($id); public function add(Clinic $clinic); }
  7. @cocoders <?php namespace Cocoders\MedicalClinic\UseCase; use Cocoders\MedicalClinic\ClinicRegistry; use Cocoders\MedicalClinic\Employee\EmploymentPositionList; final class

    HireEmployee { private $employmentPositionList; private $clinicRegistry; public function __construct( EmploymentPositionList $employmentPositionList, ClinicRegistry $clinicRegistry ) { $this->employmentPositionList = $employmentPositionList; $this->clinicRegistry = $clinicRegistry; }
  8. @cocoders <?php namespace Cocoders\MedicalClinic\UseCase; use Cocoders\MedicalClinic\ClinicRegistry; use Cocoders\MedicalClinic\Employee\EmploymentPositionList; final class

    HireEmployee { //previous slide public function execute(HireEmployee\Command $command) { $employee = $this->employmentPositionList->hireAtPosistion($command- >employmentPosistion, $command->employmentParameters); $clinic = $this->clinicRegistry->find($command->clinicId); $clinic->hireEmployee($employee); } }
  9. @cocoders namespace Cocoders\InMemoryAdapter\MedicalClinic; use Cocoders\MedicalClinic\Clinic; use Cocoders\MedicalClinic\ClinicRegistry as ClinicRegistryInterface; final

    class ClinicRegistry implements ClinicRegistryInterface { private $clinics = []; /** * @param $taxIdNumber * @return Clinic */ public function find($id) { if (isset($this->clinics[$id])) { return $this->clinics[$id]; } } public function add(Clinic $clinic) { if ($this->find($clinic->getId())) { return; } $this->clinics[$clinic->getId()] = $clinic; } }
  10. @cocoders $clinicRegistry = new \Cocoders\InMemoryAdapter\ClinicRegistry(); $clinicRegistry->add(new Clinic(1)); $hireEmployee = new

    HireEmployee( new EmploymentPositionList(), $clinicRegistry ); $hireEmployee->execute(new \Cocoders\MedicalClinic\UseCase\HireEmployee\Command( $clinicId = 1, $position = 'receptionist', $parameters = ['911-11-10-111'] ));
  11. @cocoders $clinicRegistry = new \Cocoders\MysqlAdapter\ClinicRegistry($connection); $clinicRegistry->add(new Clinic(1)); $hireEmployee = new

    HireEmployee( new EmploymentPositionList(), $clinicRegistry ); $hireEmployee->execute(new \Cocoders\MedicalClinic\UseCase\HireEmployee\Command( $clinicId = 1, $position = 'receptionist', $parameters = ['911-11-10-111'] ));