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 ?

Gagantous
  • 432
  • 6
  • 29
  • 69
Vaibhav Singhal
  • 511
  • 1
  • 4
  • 25

1 Answers1

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

Saravanan
  • 1,879
  • 2
  • 27
  • 30
  • is it possible to call function of another controller to the different controller @Saravanan – always-a-learner Jul 19 '17 at 12:42
  • @always-a-learner, you can refer this question https://stackoverflow.com/questions/14165895/how-to-load-a-controller-from-another-controller-in-codeigniter. 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 '17 at 05:52