
PHP Interfaces and Type Hinting
This article was originally published on bmf-tech.com . Overview This article is part of the PHP Advent Calendar 2018 . (Posted a bit earlier) Interfaces not only serve as a "contract" to ensure method implementation, but they also allow implementations to depend on abstractions through type hinting, making it easier to switch implementations. Defining and Implementing Interfaces Basic definition and implementation of an interface. <?php interface Action { public function say (); } class Superman implements Action { public function say () { echo "Hello World" ; } } $obj = new Superman (); $obj -> say (); Separation of Functionality and Implementation with Interfaces Specifying an interface type with type hinting allows for flexibility in implementation. <?php interface HeroAction { public function say (); } class Superman implements HeroAction { public function say () { echo "I'm a Superman" ; } } class Human { public function say () { echo "I'm a Human" ; } } class Bot { public functi
Continue reading on Dev.to
Opens in a new tab


