Last active
November 24, 2016 18:14
-
-
Save mathiasverraes/d5a8cbe3a987d2d7a67e7779b5bc6637 to your computer and use it in GitHub Desktop.
Instanceof rant
This file contains 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 | |
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