PHP类继承引用codeigniter中的父类

问题描述:

this is my problem. I'm using codeigniter and this is my structure. I have 2 classes in 2 files

class Model_setup extends CI_Model 
{
    //functions

    // an update function
    public function update()
    {
        // update stuff
    }
}

// OTHER FILE

class Tests extends Model_setup
{
    // other functions....

    // a totally different update function
    public function update()
    {
        // update a specific set of stuff
    }
}

I want to know if and how it is possible to reference these two separate update functions. In a separate controller from these two, say the Places_Controller how would you tell the difference between these two class methods and how would you make sure that you are only using one or the other of the two updates? Thank you for the help in advance.

这是我的问题。 我正在使用codeigniter,这是我的结构。 我在2个文件中有2个类 p>

 类Model_setup扩展CI_Model 
 {
 //函数
 
 //更新函数
公共函数更新()\  n {
 //更新内容
} 
} 
 
 //其他文件
 
class测试扩展了Model_setup 
 {
 //其他函数.... 
 
 //完全 不同的更新功能
公共功能更新()
 {
 //更新一组特定的东西
} 
} 
  code>  pre> 
 
 

我想知道 是否以及如何引用这两个单独的更新函数。 在这两个单独的控制器中,比如 Places_Controller code>,您如何区分这两种类方法,以及如何确保只使用这两种更新中的一种或另一种? 感谢您提前获得帮助。 p> div>

So I was enlightened by a friend about how to solve this. This doesn't need any codeigniter frmaework stuff to be made to work correctly. The following would work correctly:

class Model_setup extends CI_Model 
{
    //functions

    // an update function
    public function update()
    {
        // update stuff
    }
}

// OTHER FILE

class Tests extends Model_setup
{
    // other functions....

    // a reference function to the parent function
    public function parent_update($x)
    {
        // update a specific set of stuff
        parent::update($x);
    }

    // a totally different update function
    public function update()
    {
        // update stuff
    }
}

Now from the outside world, say another controller in the framework you can call the following once everything has been loaded. $this->tests_model->parent_update($x) when you wish to call the parent version and $this->tests_model->update when you wish to call the update function for the Tests model. This worked and I have tested this.

Assuming you're loading both models, you just reference them by name:

$this->Model_setup->update();

will refer to that first method, while

$this->Tests->update();

will refer to the second one.