Call function A inside function B on Codeigniter's controller

Call function A inside function B on Codeigniter's controller

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Calling Class Methods Within CodeIgniter Controllers

In CodeIgniter, you can absolutely call one method from another within the same controller class. The syntax is straightforward: use $this-> followed by the method name. If your controller extends CI_Controller, as shown in your snippet, the internal structure allows direct invocation between public methods without needing to instantiate the class again.

Consider this example where xyz() calls abc():

public function xyz()
{
    // Your existing logic here
    $this->abc();
}

public function abc()
{
    // Logic specific to 'abc'
}

This pattern works because both methods share the same instance of the controller. When xyz() executes, $this refers to that same object, so calling $this->abc() simply invokes the method on the current instance. This is efficient and avoids unnecessary overhead.

However, there are important nuances to keep in mind when structuring your controllers this way.

State Management and Side Effects

When you call one method from another, any changes made in abc()—such as modifying a database record, setting session data, or loading models—are reflected immediately within the context of xyz(). This can be powerful for modular logic but introduces subtle state management risks. If abc() has side effects that aren't reversible or explicitly documented, xyz() may behave unexpectedly if called multiple times or in different contexts.

For instance, if abc() loads a model and performs an update without resetting certain variables, those changes persist until another request resets them. This can lead to bugs that are hard to trace because the failure isn't isolated to a single method but rather spans multiple calls within the same request lifecycle.

Dependency on Request Context

Another consideration is whether abc() relies on data specific to the current request. In CodeIgniter, superglobal arrays like $_GET, $_POST, or session data are accessed via $this->input and $this->session. These are tied to the current HTTP request. If abc() accesses them directly, it assumes it's being called during a valid request context. While this is typically true when invoked from another controller method, it becomes problematic if you ever refactor the code to call abc() outside of a controller (e.g., in a CLI script or plugin).

To mitigate this, encapsulate request-specific logic inside the method that calls abc(), and keep abc() focused on its core responsibility—perhaps performing a calculation or retrieving shared data without relying on transient input.

Avoiding Deep Method Chaining

While calling one method from another is fine for simple reuse, deep chains of internal calls (e.g., xyz() → abc() → def()) can obscure the flow of execution and make debugging harder. Each layer adds abstraction distance between the entry point and the actual work being done.

A better practice is to break complex logic into smaller, well-defined units that accept necessary parameters rather than relying on shared state or implicit dependencies. For example:

public function xyz()
{
    $data = $this->prepareData();
    $result = $this->processData($data);
    return $this->renderResponse($result);
}

This approach makes each method's contract explicit and reduces hidden dependencies.

When to Use External Calls

If you find yourself needing to reuse logic across multiple controllers or even outside the MVC structure, consider extracting that logic into a library or model instead of keeping it within the controller. This improves testability and separation of concerns. For example:

// In models/libs
class DataProcessor {
    public function process($input) { /* ... */ }
}

Then inject or load this in your controller as needed. This keeps controllers thin and focused on request handling rather than business logic.

Final Notes

Calling $this->abc() from within xyz() is valid and commonly used in CodeIgniter for organizing code into logical blocks. Just remember to be mindful of shared state, request context, and the risk of over-abstraction. Keep your methods cohesive, avoid deep call chains, and extract reusable logic where appropriate.

For more modern PHP frameworks and patterns, you might explore how Laravel handles similar concerns at https://laravelcompany.com.

Our network