Interfaces are instructions for creating your classes. We cannot create a body of our methods in the interface
Lets create interfaces for our imaginary character properties:
interface Multiplyable{ //create a property that will be needed in the future will be described in the class that uses this interface public function multiple(int $mult); } interface Transportable{ public function moveTo(); } interface Destroyable{ public function destroy(); } interface LiveUnit{ public function makeAction(); }
Like classes, one interface can inherit another:
interface IOnGameCard extends Multiplyable, Transportable, Destroyable, LiveUnit{ }
Now let’s use our class interfaces:
class Animal implements LiveUnit, Transportable, Destroyable{ public function moveTo(){} public function destroy(){} public function makeAction(){} } class Wall implements Destroyable{ public function destroy(){} } class Stone implements Transportable, Destroyable{ public function moveTo(){} public function destroy(){} } class Seaweed implements Transportable, Destroyable, Multiplyable{ public function moveTo(){} public function destroy(){} public function multiple(){} }
Interfaces in PHP. Code examples
May 17, 2022