php 多态性 - 在多态子函数中调用父函数
这是父类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您正在寻找使用
parent
关键字:I think you're looking to use the
parent
keyword:您必须使用
parent
Event::getLang($lang);
实际上试图静态调用getLang
。请参阅以下链接:范围解析运算符 (::)
父级
You have to use
parent
Event::getLang($lang);
is infact trying to callgetLang
statically. See these links:Scope Resolution Operator (::)
Parent
该方法不是静态的,因此您需要调用父方法
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 methodUpdate:
I meant, with
Event::getLang()
you usually call a static method on a class, that may or may not be extended. Whereparent::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.