php 多态性 - 在多态子函数中调用父函数

发布于 2024-10-18 16:46:02 字数 655 浏览 2 评论 0原文

这是父类:

    class Event {

    public function getLang($lang){
     $sql = "select * from Event where EventID =" . $this->EventID . "AND Lang =" . $lang;
    $result = $this->selectOneRow($sql);
    }
}

这是子类:

class Invitation extends Event{

public function getLang($lang){
Event::getLang($lang);

$sql = "select * from invitation where EventID =" . $this->EventID . " and Lang = " . $lang;

    }
}

我曾希望 EVENT::getLang($lang) 能够正常工作,但在我回显查询后,我可以看到它停止了 EventID。

有正确的方法吗?

我尝试直接复制/粘贴子级中的代码,但这也不起作用,因为我在父级级别获得了变量,事件选择的结果将分配给这些变量。

有什么办法可以解决这个问题还是我陷入了僵局?

Here's the parent class:

    class Event {

    public function getLang($lang){
     $sql = "select * from Event where EventID =" . $this->EventID . "AND Lang =" . $lang;
    $result = $this->selectOneRow($sql);
    }
}

and here's the child:

class Invitation extends Event{

public function getLang($lang){
Event::getLang($lang);

$sql = "select * from invitation where EventID =" . $this->EventID . " and Lang = " . $lang;

    }
}

I had some hope that EVENT::getLang($lang) would work but after I echo the query, I can see that it stops short of an EventID.

Is there a right way to do this?

I tried copy/pasting the code in the child directly but that can't work either because, I got variables at the parent's level to which the result of event's select will be assigned.

Is there any way to work around this or am I in a gridlock?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人生百味 2024-10-25 16:46:02

我认为您正在寻找使用 parent 关键字:

class Invitation extends Event{
    public function getLang($lang){
       parent::getLang($lang);

       $sql = "SELECT * FROM invitation WHERE EventID =" . $this->EventID . " AND Lang = " . $lang;
    }
}

I think you're looking to use the parent keyword:

class Invitation extends Event{
    public function getLang($lang){
       parent::getLang($lang);

       $sql = "SELECT * FROM invitation WHERE EventID =" . $this->EventID . " AND Lang = " . $lang;
    }
}
情定在深秋 2024-10-25 16:46:02

您必须使用 parent

class Invitation extends Event{

    public function getLang($lang){
        parent::getLang($lang);
        ....
    }
}

Event::getLang($lang); 实际上试图静态调用 getLang 。请参阅以下链接:

范围解析运算符 (::)

父级

You have to use parent

class Invitation extends Event{

    public function getLang($lang){
        parent::getLang($lang);
        ....
    }
}

Event::getLang($lang); is infact trying to call getLang statically. See these links:

Scope Resolution Operator (::)

Parent

小傻瓜 2024-10-25 16:46:02

该方法不是静态的,因此您需要调用父方法

parent::getlang($lang);

Update, 而不是调用静态方法 Event::getLang()
我的意思是,使用 Event::getLang() 您通常会调用类上的静态方法,该方法可能会或可能不会扩展。其中 parent::method() 始终调用继承的方法并保留调用方法的范围(类或静态),Classname::method() 始终尝试调用特定类的静态方法。

The method is not static, so instead of calling the static method Event::getLang(), you need to call parents method

parent::getlang($lang);

Update:
I meant, with Event::getLang() you usually call a static method on a class, that may or may not be extended. Where parent::method() calls always the inherited method and keeps the scope (class or static) of the calling method, Classname::method() always tries to call a static method on the specific class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文