Call function A inside function B on Codeigniter's controller
Asked Answered
S

2

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 ?

Sandstrom answered 31/10, 2013 at 8:15 Comment(2)
Why would you think it is not possible? did you even try it?Confuse
yes, i try this code but not run.Sandstrom
W
43

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

Worldly answered 31/10, 2013 at 8:23 Comment(2)
is it possible to call function of another controller to the different controller @WorldlyWhom
@always-a-learner, you can refer this question #14166395. 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.Worldly
A
0

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;
  }

}
Albania answered 22/2 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.