Created
November 24, 2016 18:24
-
-
Save mathiasverraes/062d24cc98ace7f724f1f2d0f0d143eb to your computer and use it in GitHub Desktop.
faking method overloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// method overloading example (not possible in php) | |
final class SomeEventListener { | |
public function when(EmployeeWasHired $event) { | |
// set a salary | |
} | |
public function when(EmployeeWasPromoted $event) { | |
// increase salary | |
} | |
} | |
// Faking method overloading. Noisy but good enough. | |
final class SomeEventListener { | |
public function when(Event $event) { | |
switch (true) { | |
case $event instanceof EmployeeWasHired: | |
return $this->whenEmployeeWasHired($event); | |
case $event instanceof EmployeeWasPromoted: | |
return $this->whenEmployeeWasPromoted($event); | |
} | |
} | |
public function whenEmployeeWasHired(EmployeeWasHired $event) { | |
// set a salary | |
} | |
public function whenEmployeeWasPromoted(EmployeeWasPromoted $event) { | |
// increase salary | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I also like a more generic approach