首页 > 解决方案 > 为什么在PHP中这样使用时在子类中找不到父类的函数:$this->function_name()

问题描述

我正在查看一些 PHP 代码。有一个类 LQDM_Shortcodes_Run_Base 扩展了 WDS_Shortcodes。然后它尝试使用父类的公共函数att。在 PhpStorm 中,它提醒我

att找不到方法LQDM_Shortcodes_Run_Base

为什么找不到?子类确实在其自己的构造函数中调用父类的构造函数:

public function __construct( LQDM_Sermons $sermon ) {
  $this->sermons = $sermons;
  parent::__construct();
}

子类中的调用如下所示:

protected function get_sermon() {
  $sermon_id = $this->att( 'sermon_id' );
  ...
}

父类的函数如下所示:

class WDS_Shortcodes {
  ...
  public function att( $att, $default = null ) {
    ...
  }
  ...
}

标签: phpfunctionclass

解决方案


推荐阅读