usumoio
usumoio

Reputation: 3568

PHP Class inheritance referencing the parent class in 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.

Upvotes: 0

Views: 1033

Answers (2)

usumoio
usumoio

Reputation: 3568

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.

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 219930

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.

Upvotes: 1

Related Questions