Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using interfaces in PHP

Tags:

php

I am coming from C# and I am struggling to work out the PHP way of making my code extensible.

Lets say for instance I have a function that calculates my recommended daily allowance of food intake. It looks at vitamins, calories, minerals etc...

I want to have lots of different food objects that I use in my application and different functions that look at different aspects of food so I implement a IFoodNutrition interface and make my food classes that I want to be compatible with my function implement it. I then declare that my function takes it as an arg

function GetRda(IFoodNutrition $food){}

In C# I would specify all of the properties that I need to use in the interface so I know any class that implements this will be compatible with this function.

What is making my head hurt is that PHP only allows method signatures in interfaces.

My problems is obviously the paradigm shift. I am not sure what the equivalent pattern is in PHP. I have read the documentation and I can't see the like for like alternative unless you inherit from an abstract class but then you can only inherit from one class so that doesn't work the same way.

As PHP isn't typesafe I don't have to create the interface. Someone can just send any object to the method and if it has the properties needed in the right format it will work. This just doesn't seem right to me though as I am so used to laying out how things should be interacted with in interfaces.

I feel like there should at least be some extra step in the middle where the object is validated and some obvious structure that a developer can look at and then implement their own class intuitively and see errors before runtime.

Is there a specific pattern in PHP like this?

like image 706
Guerrilla Avatar asked Apr 16 '15 20:04

Guerrilla


People also ask

How is interface used in PHP?

A PHP interface defines a contract which a class must fulfill. If a PHP class is a blueprint for objects, an interface is a blueprint for classes. Any class implementing a given interface can be expected to have the same behavior in terms of what can be called, how it can be called, and what will be returned.

Why should I use interface in PHP?

PHP - What are Interfaces? Interfaces allow you to specify what methods a class should implement. Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as "polymorphism".

Can I implement multiple interfaces in PHP?

implements ¶Classes may implement more than one interface if desired by separating each interface with a comma. A class can implement two interfaces which define a method with the same name, only if the method declaration in both interfaces is identical.

WHAT IS interface in OOP with example?

An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X".


1 Answers

PHP only allows method signatures in interfaces.

This is true. However, nothing is stopping you from making traditional getters and setters for properties as part of the interface. Doing so abstracts underlying implementation details.

Someone can just send any object to the method and if it has the properties needed in the right format it will work.

This is not true. You have type-hinted the argument with IFoodNutrition $food. PHP will throw a fatal error if this function receives an object that does not implement the IFoodNutrition interface.

like image 198
Jason McCreary Avatar answered Oct 21 '22 06:10

Jason McCreary