Skip to content

Instantly share code, notes, and snippets.

@mathiasverraes
Last active November 24, 2016 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathiasverraes/d5a8cbe3a987d2d7a67e7779b5bc6637 to your computer and use it in GitHub Desktop.
Save mathiasverraes/d5a8cbe3a987d2d7a67e7779b5bc6637 to your computer and use it in GitHub Desktop.
Instanceof rant
<?php
final class Foo {
public function bar (AnemicObject $object) {
switch (true) {
case $object instanceof Alfa:
// do alpha shizzle
case $object instanceof Beta:
// do beta shizzle
}
}
}
// refactor to:
final class Foo {
public function bar (PolymorphicThing $object) {
$object->doShizzle();
}
}
interface PolymorphicThing {
public function doShizzle();
}
final class Alfa implements PolymorphicThing {
public function doShizzle() {
// move alpha shizzle here
}
}
final class Beta implements PolymorphicThing {
public function doShizzle() {
// move beta shizzle here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment