Php 从扩展类调用函数
从extended,我想调用这样的父函数 文件1>主类:Php 从扩展类调用函数,php,Php,从extended,我想调用这样的父函数 文件1>主类: class CheckoutProcessCore implements Interface { public function __construct( Context $context, CheckoutSession $checkoutSession ) { $this->context = $context; $this->smarty =
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;
}
}
class Basket extends CheckoutProcessCore
{
protected static $basket_pid = null;
function __construct() {
parent::__construct();
}
public static function test() {
$step = parent::getSteps(); <-- the Problem!
[ ... ]
}
}
文件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;
}
}
class Basket extends CheckoutProcessCore
{
protected static $basket_pid = null;
function __construct() {
parent::__construct();
}
public static function test() {
$step = parent::getSteps(); <-- the Problem!
[ ... ]
}
}
类篮扩展了CheckoutProcessCore
{
受保护的静态$basket_pid=null;
函数_u构造(){
父项::_构造();
}
公共静态功能测试(){
$step=parent::getSteps();我不确定这是否有助于/解决您的问题。但是
- 如果未使用autholoader/composer,您可能忘记了将文件添加到页面顶部,而这可能是执行此操作所必需的:
require_once('/path/to/CheckoutProcessCore.php');
require_once('/path/to/Basket.php');
- 此外,您可能不允许使用
接口
作为标识符,您可以查看该标识符,即接口
是保留字。您很可能无法使用接口
的其他变体,例如(接口
,接口
等)作为标识符
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;
}
}
您的Basket
-类扩展了CheckoutProcessCore
,而getSteps()
-方法在类核心中
,因此就发布的代码而言,您的两个类在任何形式或形式上都不相关。如果您不回复我们的评论,我们将无法帮助您。您收到的错误消息是什么?@Magnus:我在代码中犯了一个错误,现在我更改了它。@Progman:错误显示出来了“FatalThroTableError-不在对象上下文中时使用$this”