php 多态性 - 在多态子函数中调用父函数 - get_class($this) 问题
我创建了一个函数,用于自动从相关类中可用的任何匹配变量中的选择中分配值
class FR_Object{
protected $me;
public function __construct()
{
$this->me = new ReflectionClass($this);
}
public function setFrom($data)
{
if (is_array($data) && count($data)) {
$valid = get_class_vars(get_class($this));
foreach ($valid as $var => $val) {
if (isset($data[$var])) {
$this->$var = $data[$var];
}
}
}
}
}
父类:
class FR_Event extends FR_Object_DB {
public $EventsID;
public $Subject;
public $Description;
public $UserID;
public $Username;
public $Address;
public $Longitude;
public $Latitude;
public $EventStartDate;
public $EventEndDate;
public $EventsTypeID;
public $CountryID;
public $CityID;
public $Status;
public $Lang;
public $When;
public function getLang($lang){
$sql = "SELECT e.EventsID, Subject, Description, UserID, Username, Address, Longitude, Latitude, EventStartDate, EventEndDate, EventsTypeID, CountryID, CityID, Status, Lang
FROM Events e
INNER JOIN EventsDetails d
ON e.EventsID = d.EventsID
WHERE e.EventsID = " . $this->EventsID .
" AND Lang = " . $lang;
$result = $this->selectOneRow($sql);
if ($result) {
$this->setFrom($result);
}else{
$this->Lang = null;
$this->Subject = null;
$this->Description = null;
}
}
}
子类:
class FR_Invitation extends FR_Event{
public $invites;
public function getLang($lang){
parent::getLang($lang);
$sql = "SELECT InvitationID, Email, InvitationDate, ConfirmationName, UserID
FROM Invitation i
INNER JOIN ConfirmationStatus s on i.ConfirmationStatusID = s.ConfirmationStatusID
WHERE EventsID = " . $this->EventsID .
" AND Lang = " . $this->Lang;
$this->invites = $this->select($sql);
}
}
我的问题是父类中的函数在子类中调用,如下所示:
parent::getLang( $郎)。
Parent::getLang($lang) 使用 setFrom 但此时 $this 不再是父类。
是孩子!
是否有人找到一种方法来更改此函数以供正常使用以及通过parent:: 调用时,或者我是否必须将其扔到垃圾箱并采用更传统(和更长)的方法?
I created a function to automatically assign values from a select in whatever matching variables available in the related class
class FR_Object{
protected $me;
public function __construct()
{
$this->me = new ReflectionClass($this);
}
public function setFrom($data)
{
if (is_array($data) && count($data)) {
$valid = get_class_vars(get_class($this));
foreach ($valid as $var => $val) {
if (isset($data[$var])) {
$this->$var = $data[$var];
}
}
}
}
}
Parent class:
class FR_Event extends FR_Object_DB {
public $EventsID;
public $Subject;
public $Description;
public $UserID;
public $Username;
public $Address;
public $Longitude;
public $Latitude;
public $EventStartDate;
public $EventEndDate;
public $EventsTypeID;
public $CountryID;
public $CityID;
public $Status;
public $Lang;
public $When;
public function getLang($lang){
$sql = "SELECT e.EventsID, Subject, Description, UserID, Username, Address, Longitude, Latitude, EventStartDate, EventEndDate, EventsTypeID, CountryID, CityID, Status, Lang
FROM Events e
INNER JOIN EventsDetails d
ON e.EventsID = d.EventsID
WHERE e.EventsID = " . $this->EventsID .
" AND Lang = " . $lang;
$result = $this->selectOneRow($sql);
if ($result) {
$this->setFrom($result);
}else{
$this->Lang = null;
$this->Subject = null;
$this->Description = null;
}
}
}
child class:
class FR_Invitation extends FR_Event{
public $invites;
public function getLang($lang){
parent::getLang($lang);
$sql = "SELECT InvitationID, Email, InvitationDate, ConfirmationName, UserID
FROM Invitation i
INNER JOIN ConfirmationStatus s on i.ConfirmationStatusID = s.ConfirmationStatusID
WHERE EventsID = " . $this->EventsID .
" AND Lang = " . $this->Lang;
$this->invites = $this->select($sql);
}
}
My problem is that a function in a parent class is called in a child class like this:
parent::getLang($lang).
parent::getLang($lang) uses setFrom but at that point $this is not the parent class anymore.
It's the child!
Does someone see a way to alter this function for normal usage and when called by way of parent:: or do I have to just throw it to the bin and go for more traditional (and longer) methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白你为什么不能保持原样。在这种情况下,唯一的结果是 $invites 也将由 get_class_vars() 调用返回。
执行您正在讨论的行为的一个选项可能是将可选参数传递给
setFrom()
函数。然后在您的父级中,Event 类使用对 setFrom 的调用。
I don't see why you couldn't leave it as is. The only consequence in that case would be that $invites would also be returned by
the get_class_vars()
call.An option to do the behaviour you are talking about could be to pass an optional parameter to the
setFrom()
function.Then in your parent, Event class use this call to setFrom.
只需不要覆盖子类中的
setForm
函数,它将使用其扩展的父类中的setForm
函数。然而,它似乎对OO继承有明显的误解。您最好花时间阅读相关的文档。
您需要使用 setter 注入(google 一下)并传递 get_class_vars 的结果或调用类名。
Simply don't overwrite the
setForm
function in the child class and it will use thesetForm
in the parent class it is extending.However, it seems to have a clear misunderstanding of OO inheritance. Best you take the time read through the relevant documentation.
You need to use setter injection (google it) and pass either the result of
get_class_vars
or the calling class name.