Mythos33
4 years ago
527
5
General

Generics equivalent in PHP

Posted 4 years ago by Mythos33

Hello guys,

I'm trying to use Repositories which implement my RepositoryInterface. As a C# .NET developer I would create a Generic interface like this:

public interface IRepository<TModelType>
{
    IEnumerable<TModelType> FindAll();
    void Save(TModelType model);
}

I would implement it like this:

public class UserRepository : IRepository<User>
{
    public IEnumerable<User> FindAll()
    {
        // Method body
    }

    public void Save(User model)
    {
        // Method body
    }
}

Now my question: Is there something like this in PHP?

EDIT:

The reason I want to do this is type safety and code completion assistance and I don't want to cast the model parameter all the time. ;-)

Regards, Stefan

Please sign in or create an account to participate in this conversation.