从扩展类调用函数

时间:2019-04-28 10:33:52

标签: php

从扩展名开始,我想像这样调用父函数

文件1>主类:

 public void RegisterRoutes(IRouteBuilder routes)
 {
    var httpContextAccessor = EngineContext.Current.Resolve<IHttpContextAccessor>();
    var domainName = httpContextAccessor.HttpContext.Request.Host.Value.ToString();

    var pluginValidtyChecker = new PluginValidityChecker.ValidityChecker();
    if (pluginValidtyChecker.CheckIsValid(domainName))
    {

     //Register routes

    }
 }

文件2>扩展类:

class CheckoutProcessCore implements Interface
{
    public function __construct(
        Context $context,
        CheckoutSession $checkoutSession
    ) {
        $this->context = $context;
        $this->smarty = $this->context->smarty;
        $this->checkoutSession = $checkoutSession;
    }

    public function getSteps()
    {
        return $this->steps;
    }
}

如何从测试中调用getSteps而不出现任何错误? 我的代码有什么问题?

谢谢

1 个答案:

答案 0 :(得分:0)

我不确定这是否可以帮助/解决您的问题。但是,

  • 如果您不使用autholoader / composer,则可能忘记了在可能需要这样做的页面顶部添加文件:

    require_once('/path/to/CheckoutProcessCore.php');
    require_once('/path/to/Basket.php');
    
  • 此外,您可能不被允许使用Interface作为标识符,为此您可以查看this post,它表示Interface是保留字。您极不可能使用Interface的其他方差,例如({interfaceiNterface等)作为标识符。

    require_once('/path/to/InterfaceSomeAddedNameThatYouWish.php');
    

然后,您可以考虑修改:

class CheckoutProcessCore implements InterfaceSomeAddedNameThatYouWish
{
    public function __construct(
        Context $context,
        CheckoutSession $checkoutSession
    ) {
        $this->context = $context;
        $this->smarty = $this->context->smarty;
        $this->checkoutSession = $checkoutSession;
    }

    public function getSteps()
    {
        return $this->steps;
    }
}