我的课堂功能执行了两次 [英] My class function is performed twice

查看:73
本文介绍了我的课堂功能执行了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
  /*
  * class: C_QUESTION_MULTIPLE_CHOICE_SINGLE_ANSWER
  * properties:
  * functions:
  */

  class C_QUESTION_MULTIPLE_CHOICE_SINGLE_ANSWER extends C_QUESTION {
    protected $q_type = 'T_MULTIPLE_CHOICE_SINGLE_ANSWER';

    protected $q_type_info;
    /*
    * $choices:
    * choices['A'] = array(answer = 'Y', description = '')
    * choices['B'] = array(answer = 'N', description = '')
    */      
    protected $choices; 

    public $output_info = '';
    public $count = 0;


    function __construct($q_content, $option = 'load') {
        // call parent's construct function to handle question base class information
        // in parent construct function, it will call parent's load or save function by itself.

        parent::__construct($q_content, $option);
        if (! $this->op_status) {
            // call function failed
            return;
        }   


        // handle multiple choice part
        switch ($option) {    
            case 'save':
            // $option = false, don't call parent::save() function
            // parent construct function will call parent save function
              $this->save($q_content, false); 
              break;
        }
    } // end of construct function

    function save($q_content, $option = true) {
        $this->count++;
        $this->output_info .= '</br>'.$this->count.'</br>';
    } // end of save function


  }

在上面的代码中,子类函数保存执行了两次.如果我不调用父构造函数:

In the above code, the sub-class function save is performed twice. If I don't call parent construct function:

/* don't call parent construct function
        parent::__construct($q_content, $option);
        if (! $this->op_status) {
            // call function failed
            return;
        }  
*/

函数save()将执行一次.

The function save() will be performed once.

在父构造函数中,父类QUESTION将对其自身的save()函数进行校准.子类C_QUESTION_MULTIPLE_CHOICE_SINGLE_ANSWER覆盖了父函数save().因此,在子类构造函数中,我手动调用了parent :: __ construct()函数.但是我不解释为什么子类save()函数被调用两次.我通过子类属性对此进行调试
public $ output_info ='';公开的$ count = 0;

In the parent construct function, the parent class QUESTION will cal it's own save() function. The sub-class C_QUESTION_MULTIPLE_CHOICE_SINGLE_ANSWER override the parent function save(). So in the sub-class construction function, I manually call parent::__construct() function. But I don't why the sub-class save() function is called twice. I debug this by sub-class properties
public $output_info = ''; public $count = 0;

请帮助我.

非常感谢.

=======================================================================================

=========================================================================================

发布上述问题后,我进行了几次调试,发现:

I debuged several times after post the above question, found:

父类问题:__construct()函数:

parent class QUESTION: __construct() function:

函数__construct($ q_content,$ option ='load'){
//将操作状态设置为true.如果此操作失败,则将其设置为false$ this-> op_status = true;

function __construct($q_content, $option = 'load') {
// set the operation status to true. If this operation fails, this will be set to false $this->op_status = true;

switch ($option) {
    case 'load':
      if (!$q_content['qid']) {
        $this->op_status = false;
        drupal_set_message(t('question id :qid not exist. Please contact site administrator!', array(':qid' => $q_content['qid'])), 'error');

        break;
      }

      $this->basic['qid'] = $this->extra['qid'] = $q_content['qid'];
      $this->load($this->basic['qid']);

      break;

    case 'save':
      **//$this->save($q_content);
      self::save($q_content);**

      break;
}       

}

如果我使用$ this-> save调用parent本身的保存函数,则该语句不仅调用parent :: save函数,还将调用sub class :: save函数.这很有趣而且令人困惑.

if I use $this->save to call parent itself save function, this statement not only call parent::save function, but also will call sub class::save function. It's very funny and confuse.

如果我在父类QUESTION中使用self :: save语句,那么它将不会调用子类:: save函数.

If I use self::save statement in the parent class QUESTION, then it won't call sub class::save function.

我的问题是,因为我已经在子类中重写了parent :: save函数,所以我应该调用parent :: save,然后在子类中调用$ this-> save为子类做一些事情.为什么当我调用parent :: save时,它将同时执行parent :: save和子类::: save.这真的让我感到困惑.

My question is, since I've already overrided parent::save function in the sub class, I should call parent::save, then call $this->save in the sub-class do something for subclass. Why when I call parent::save, it will perform both parent::save and sub-class::save. It really confuse me.

我试图在php手册和Internet中找到答案,但是我没有找到相关的文章可以使我清楚.他们只说self :: function()用于静态成员函数,而$ this-> function()用于其他成员和函数.但是我的功能和成员不是静态的.还是默认的函数和成员是静态的?

I tried to find answer in the php manual and internet, but I didn't find the relevant article can make me clear. They only say, self::function() is used for the static member function, and $this->function() is used for other member and function. But my function and member are not static. Or the default function and member are static?

真的感谢能帮助我的人.

Really appreciate for who can help me.

非常感谢.

推荐答案

在php OOP中, $ this-> 引用当前的 instant 该类的strong>,而 self :: 引用到声明了 self :: 的类. self :: 也用于引用静态功能和属性.

In php OOP, $this-> referrer to the current instant of the class, whereas self:: referrer to the class where self:: is declared.self:: is also use to referrer to static functions and properties.

因此,在您的情况下,您必须在父级的构造函数中将对父级 save()函数的调用更改为 self :: save().

So, in your case, you have to change the call to your parent's save() function as self::save() within the parent's construct function.

此外,在子类中,您可以调用 $ this-> save()来调用子级的save函数,因为您将覆盖父级的save函数.子类中的 self :: save().如果要从子类中调用父级的save函数,则可以在子类中使用 parent :: save()课.

Also, within your child class, you can call $this->save() to call the save function of the child, because you are overriding the parent's save function.you can also use self::save() within the child class.if you want to call parent's save function from a child class then you can use parent::save() in your child class.

否,默认函数不是静态的.要使其变为静态,必须在 function 声明的前面添加 static 关键字.请注意,您不能声明 __ construct() __ deconstruct()为静态.

No, default functions aren't static.to make them static you have to add static keyword in front of the function declaration.note that you can't declare __construct(),__deconstruct() as static.

由于您的函数不是静态的,因此当您将它们引用为 self :: 时,就是在指代当前类.它称为 SO答案

Since your functions aren't static, when you referrer to them as self:: you are referring to the current class.it's called Scope Resolution Operator (::). also read this SO answer

这篇关于我的课堂功能执行了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆