如果我在 PHP 中有一个接口,请说
interface AuthenticationInterface {
}
Run Code Online (Sandbox Code Playgroud)
和两个实现接口的类,例如
class ApiKey implements AuthenticationInterface {
}
class AuthToken implements AuthenticationInterface {
}
Run Code Online (Sandbox Code Playgroud)
在实例化新的 AuthenticationInterface 时,如何确定使用这些类中的哪一个?
接口用于为继承它的类定义结构(架构)。
接口是一个契约,指定一组方法、字段和属性,这些方法、字段和属性将在任何实现对象上可用
我认为您对类工厂设计模式感兴趣。在此处阅读更多相关信息:php design patterns