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

标签 php object polymorphism

这是父类:

    class Event {

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

这是 child :

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。

有没有正确的方法来做到这一点?

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

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

最佳答案

我认为您希望使用 parent 关键字:

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

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

关于php 多态性 - 在多态子函数中调用父函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5077728/

相关文章:

php - 如果 PHP 的 mt_rand() 使用比 rand() 更快的算法,为什么不直接更改 rand() 以使用更新的实现呢?

c# - 欺骗 JS 对象

C++继承: Pass sub-class to a function expecting base-class and get sub-class behaviour

php - 我在 MySQL 中存储 token 时遇到困难

PHP DateTime 相等

php - 当字符串中有行 ['value' ] 时,在 PHP 中转义字符串的正确方法是什么?

javascript - 拼接数组中的嵌套对象

javascript - 将等级作为对象值从最高到最低排序

c++ - 使用编译时已知的类型避免模板冗长和动态多态性

c# - 防止事件产生另一个线程