Slide 1

Slide 1 text

Introduction to Object Oriented Programming in PHP @cocoders

Slide 2

Slide 2 text

@cocoders Who Am I? Software Developer since 2007, beer lover, Founder of Cocoders

Slide 3

Slide 3 text

What is Object Oriented Programming? @cocoders

Slide 4

Slide 4 text

State(property) + Behavior(method) = Object Set of objects communicating each other = Object oriented program @cocoders

Slide 5

Slide 5 text

employees[$employee->getTaxNumber()] = $employee; } } @cocoders

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

@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

Slide 8

Slide 8 text

@cocoders Bertrand Meyer – creator of Eiffel (1986) Design by contract

Slide 9

Slide 9 text

Communication is the key in OOP @cocoders

Slide 10

Slide 10 text

How PHP can help you with communication and OOP? @cocoders

Slide 11

Slide 11 text

Abstraction @cocoders

Slide 12

Slide 12 text

Abstract classes @cocoders

Slide 13

Slide 13 text

@cocoders namespace Cocoders\MedicalClinic; abstract class Employee { private $taxNumber; public function __construct($taxNumber) { $this->taxNumber = $taxNumber; } public function getTaxNumber() { return $this->taxNumber; } }

Slide 14

Slide 14 text

@cocoders

Slide 15

Slide 15 text

@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); } }

Slide 16

Slide 16 text

employees[$employee->getTaxNumber()] = $employee; } } @cocoders

Slide 17

Slide 17 text

@cocoders $clinic = new Clinic('ClinicId'); $clinic->hireEmployee(new Receptionist('911-11-11-111')); $clinic->hireEmployee(new Doctor( '911-11-10-111', 'AZG-11', ['neurologist'] ));

Slide 18

Slide 18 text

Interfaces @cocoders

Slide 19

Slide 19 text

@cocoders

Slide 20

Slide 20 text

@cocoders employmentPositionList = $employmentPositionList; $this->clinicRegistry = $clinicRegistry; }

Slide 21

Slide 21 text

@cocoders employmentPositionList->hireAtPosistion($command- >employmentPosistion, $command->employmentParameters); $clinic = $this->clinicRegistry->find($command->clinicId); $clinic->hireEmployee($employee); } }

Slide 22

Slide 22 text

@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; } }

Slide 23

Slide 23 text

@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'] ));

Slide 24

Slide 24 text

@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'] ));

Slide 25

Slide 25 text

WARNING: Premature abstraction @cocoders

Slide 26

Slide 26 text

Plan your abstractions and extension points carefully! @cocoders

Slide 27

Slide 27 text

Encapsulation – object should change state only in EXPECTED way @cocoders

Slide 28

Slide 28 text

@cocoders $clinic->setTaxIdNumber('9562307984'); $clinic->setName('Cocoders'); $clinic->addEmployee((new Employee())- >setFirstName('Leszek'))); $clinic->getEmployees()->add((new Employee())->setFirstName('Szymon')); vs $clinic = new Clinic('Cocoders', ' 9562307984'); $clinic->hire(new Candidate('Leszek')); $clinic->hire(new Candidate('Szymon')); Violation of encapulation

Slide 29

Slide 29 text

Encapsulation allows to control communication in our OO program @cocoders

Slide 30

Slide 30 text

How to design our objects? @cocoders

Slide 31

Slide 31 text

SOLID @cocoders

Slide 32

Slide 32 text

Cohesion & Coupling @cocoders

Slide 33

Slide 33 text

Law of Demeter @cocoders

Slide 34

Slide 34 text

Thank You! @cocoders

Slide 35

Slide 35 text

Questions? @cocoders