Vaibhav Singhal
Vaibhav Singhal

Reputation: 521

Call function A inside function B on Codeigniter's controller

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 ?

Upvotes: 21

Views: 107640

Answers (2)

Janib Soomro
Janib Soomro

Reputation: 622

Codeigniter v4: If you are adding it inside a controller method then add return as well:

class MYCONTROLLER extends BaseController {

  public function one() {
    return $this->two('Hello!');
  }

  public function two($dummy) {
    echo $dummy;
  }

}

Upvotes: 0

Saravanan
Saravanan

Reputation: 1889

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

Upvotes: 43

Related Questions