将接口发送到构造函数 PHP
Send interface to constructor PHP
我已经下载了一个支付连接示例。不,我正在尝试使用它,但是构造函数想要在我声明 ClassName 时获取接口
但我不知道该怎么做。我试过了
$interface = CallbackInterface::class;
$interface = CallbackInterface();
$interface = CallbackInterface;
还有很多,但我想不通。我唯一知道的是用 class 实现一个接口。也许是菜鸟问题,但我搜索了将近一天都没有成功。
$config = new Config('string1', 'string2');
$pay = new ClassName($config, $interface);
interface CallbackInterface
{
public function Key($sIdentifier, $sTransactionKey);
public function tSuccess($sTransactionKey);
}
class ClassName
{
public function __construct(Config $oConfig, CallbackInterface $oCallbacks)
{
$this->oConfig = $oConfig;
$this->oCallbacks = $oCallbacks;
}
}
您应该按照这些思路寻找解决方案
// Create a class that implements the interface (e.g. MyClass)
// MyClass implements the interface functions: Key and tSuccess
// MyClass can now be injected as type CallbackInterface into the __construct() of class ClassName
Class MyClass implements CallbackInterface
{
public function Key($sIdentifier, $sTransactionKey)
{
// your implementation here
}
public function tSuccess($sTransactionKey)
{
// your implementation here
}
}
interface CallbackInterface
{
public function Key($sIdentifier, $sTransactionKey);
public function tSuccess($sTransactionKey);
}
class ClassName
{
public function __construct(Config $oConfig, CallbackInterface $oCallbacks)
{
$this->oConfig = $oConfig;
$this->oCallbacks = $oCallbacks;
}
}
$config = new Config('string1', 'string2');
$interface = new MyClass(); // you've now instantiated an object of type CallbackInterface
$pay = new ClassName($config, $interface);
我已经下载了一个支付连接示例。不,我正在尝试使用它,但是构造函数想要在我声明 ClassName 时获取接口 但我不知道该怎么做。我试过了
$interface = CallbackInterface::class;
$interface = CallbackInterface();
$interface = CallbackInterface;
还有很多,但我想不通。我唯一知道的是用 class 实现一个接口。也许是菜鸟问题,但我搜索了将近一天都没有成功。
$config = new Config('string1', 'string2');
$pay = new ClassName($config, $interface);
interface CallbackInterface
{
public function Key($sIdentifier, $sTransactionKey);
public function tSuccess($sTransactionKey);
}
class ClassName
{
public function __construct(Config $oConfig, CallbackInterface $oCallbacks)
{
$this->oConfig = $oConfig;
$this->oCallbacks = $oCallbacks;
}
}
您应该按照这些思路寻找解决方案
// Create a class that implements the interface (e.g. MyClass)
// MyClass implements the interface functions: Key and tSuccess
// MyClass can now be injected as type CallbackInterface into the __construct() of class ClassName
Class MyClass implements CallbackInterface
{
public function Key($sIdentifier, $sTransactionKey)
{
// your implementation here
}
public function tSuccess($sTransactionKey)
{
// your implementation here
}
}
interface CallbackInterface
{
public function Key($sIdentifier, $sTransactionKey);
public function tSuccess($sTransactionKey);
}
class ClassName
{
public function __construct(Config $oConfig, CallbackInterface $oCallbacks)
{
$this->oConfig = $oConfig;
$this->oCallbacks = $oCallbacks;
}
}
$config = new Config('string1', 'string2');
$interface = new MyClass(); // you've now instantiated an object of type CallbackInterface
$pay = new ClassName($config, $interface);