我的班级功能执行了两次

时间:2014-04-27 00:28:10

标签: php function class

<?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


  }

在上面的代码中,子类函数save执行两次。如果我没有调用父构造函数:

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

函数save()将执行一次。

在父构造函数中,父类QUESTION将调用它自己的save()函数。子类C_QUESTION_MULTIPLE_CHOICE_SINGLE_ANSWER覆盖父函数save()。所以在子类构造函数中,我手动调用parent :: __ construct()函数。但我不知道为什么子类save()函数被调用两次。我通过子类属性来调试这个 public $ output_info =&#39;&#39 ;; public $ count = 0;

请帮帮我。

非常感谢。

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

在发布上述问题之后,我发了好几次,发现:

父类问题:__construct()函数:

function __construct($ q_content,$ option =&#39; load&#39;){
    //将操作状态设置为true。如果此操作失败,则将其设置为false     $ this-&gt; 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-&gt; save来调用parent本身的save函数,这个语句不仅调用parent :: save函数,还会调用sub class :: save函数。它非常有趣和混乱。

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

我的问题是,既然我已经在子类中覆盖了parent :: save函数,我应该调用parent :: save,然后调用$ this-&gt;保存在子类中为子类做一些事情。为什么当我调用parent :: save时,它将同时执行parent :: save和sub-class :: save。这让我很困惑。

我试图在php手册和互联网上找到答案,但我没有找到相关文章可以让我清楚。他们只说,self :: function()用于静态成员函数,$ this-&gt; function()用于其他成员和函数。但我的功能和成员不是静态的。或者默认函数和成员是静态的?

非常感谢谁能帮助我。

非常感谢。

1 个答案:

答案 0 :(得分:0)

在php OOP中,$this->引用了class.where的当前 instant self::引用了{{1}的类声明。self::也用于引用静态函数和属性。读取下面的内容。

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

并且在您的子课程中,您可以调用self::save()来调用孩子的保存功能,因为您要覆盖父母保存功能。您也可以在子课程中使用$this->save()。如果您想要要从子类调用父级的保存功能,您可以在子类中使用self::save()

不,默认功能不是静态的。要使它们保持静态,您必须在parent::save()声明之前添加static关键字。您无法声明{{1} },function为静态。

因为当你引用它们__construct()时你的功能不是静态的,你指的是当前的class.it,它被称为Scope Resolution Operator (::)。另请阅读this SO answer

相关问题