php 多态性 - 在多态子函数中调用父函数 - get_class($this) 问题

发布于 2024-10-18 13:43:16 字数 2519 浏览 1 评论 0原文

我创建了一个函数,用于自动从相关类中可用的任何匹配变量中的选择中分配值

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 技术交流群。

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

发布评论

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

评论(2

夏至、离别 2024-10-25 13:43:16

我不明白你为什么不能保持原样。在这种情况下,唯一的结果是 $invites 也将由 get_class_vars() 调用返回。

执行您正在讨论的行为的一个选项可能是将可选参数传递给 setFrom() 函数。

public function setFrom($data, $class = null)
{
    $class = $class == null ? get_class($this) : $class;
    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];
            }
        }
    }
}

然后在您的父级中,Event 类使用对 setFrom 的调用。

$this->setFrom($result, get_class());

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.

public function setFrom($data, $class = null)
{
    $class = $class == null ? get_class($this) : $class;
    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];
            }
        }
    }
}

Then in your parent, Event class use this call to setFrom.

$this->setFrom($result, get_class());
厌倦 2024-10-25 13:43:16

只需不要覆盖子类中的 setForm 函数,它将使用其扩展的父类中的 setForm 函数。

然而,它似乎对OO继承有明显的误解。您最好花时间阅读相关的文档

您需要使用 setter 注入(google 一下)并传递 get_class_vars 的结果或调用类名。

class FR_Object {
    public function setFrom($data, $vars) {
         if (is_array($data) && count($data)) {
              $valid = $vars;
              foreach ($valid as $var => $val) {
                   if (isset($data[$var])) {
                       $this->$var = $data[$var];
                   }
              }
         }
    }
}

class FR_Invitation {
    public function setFrom($data, $vars) {
        parent::setForm($data, $vars);
    }
}

Simply don't overwrite the setForm function in the child class and it will use the setForm 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.

class FR_Object {
    public function setFrom($data, $vars) {
         if (is_array($data) && count($data)) {
              $valid = $vars;
              foreach ($valid as $var => $val) {
                   if (isset($data[$var])) {
                       $this->$var = $data[$var];
                   }
              }
         }
    }
}

class FR_Invitation {
    public function setFrom($data, $vars) {
        parent::setForm($data, $vars);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文