21

I have a controller that have about 5-6 functions.

class Register extends CI_Controller {
public function index()
{
  //  some code written
}    
public function Add()
{
  //  Some code written
}
public function xyz()
{
  //  Some code written
  $this->abc();
}
public function abc()
{
  // Some code written
}
}

In xyz function, i want to call abc function. Is this possible ? if so, how to call it ?

2
  • Why would you think it is not possible? did you even try it? Oct 31, 2013 at 8:18
  • yes, i try this code but not run. Oct 31, 2013 at 8:21

1 Answer 1

42

It is possible, the code you have written is correct

public function xyz()
{
  //  Some code written
  $this->abc();     //This will call abc()
}

EDIT:

Have you properly tried this?

class Register extends CI_Controller {
    public function xyz()
    {
      $this->abc();
    }
    public function abc()
    {
      echo "I am running!!!";
    }
}

and call register/xyz

2
  • is it possible to call function of another controller to the different controller @Saravanan Jul 19, 2017 at 12:42
  • @always-a-learner, you can refer this question stackoverflow.com/questions/14165895/…. As a best practice, if you have a common code to be accessed by both controllers, move it to library and access library from each controller.
    – Saravanan
    Jul 20, 2017 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.