4

If I have an interface in PHP, say

interface AuthenticationInterface {
}

and two classes which implement the interface, for example

class ApiKey implements AuthenticationInterface {
}

class AuthToken implements AuthenticationInterface {
}

How is it determined which of these classes is used when a new AuthenticationInterface in instantiated?

3
  • 6
    You can't instantiate interfaces directly; you have to explicitly pick an implementing class to instantiate. Commented Oct 15, 2014 at 6:03
  • 2
    I don't get why you received a negative vote for the question. You are clearly new to programming and instead of receiving guidance, you get a negative mark. WTF Stackoverflow? Commented Oct 15, 2014 at 6:13
  • What is your real problem? Commented Oct 15, 2014 at 7:25

3 Answers 3

6

Interfaces are used to define a structure(architecture) for classes that inherit it.

An interface is a contract specifying a set of methods, fields and properties which will be available on any implementing object

I think you were interested in the class factory design pattern. Read more about it here: php design patterns

Sign up to request clarification or add additional context in comments.

Comments

0

Interface can't be instantiated. You can create object of class.

Comments

0

If your object was instantiated for you somewhere upstream and all you know is that it implements your AuthenticationInterface interface(so you know it can be one of a small number of classes...2 in your case), you can tell which class your object is by using "instanceof"

if ($your_object instanceof ApiKey)
{
    echo 'object class is ApiKey';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.