Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.