CodeIgniter call method from within a method in the same class

CodeIgniter call method from within a method in the same class

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

Stefan Bogdanescu

Founder & Senior Architect

Calling Methods Within the Same Class in CodeIgniter

In your specific scenario, you are asking how to invoke current_stock() from inside add_stock() within a single class definition. The answer is straightforward: you call it exactly as you would any other function in PHP, using the $this keyword to reference the current instance of the class.

Here is the direct implementation:

class Inventory extends Controller
{
    function current_stock()
    {
        // Logic to retrieve or calculate current stock levels
        $stock_data = $this->load->database('default', TRUE);
        // ... database query logic ...
        return $result;
    }

    function add_stock()
    {
        // Logic to process incoming stock data
        $new_items = $this->input->post('items');
        
        // Execute the calculation after receiving new data
        $updated_count = $this->current_stock(); 
        
        return $updated_count;
    }
}

The $this keyword is crucial here. Since PHP methods are instance-based, you must explicitly tell the interpreter to look for the method within the current object. While some languages allow calling methods directly by name, PHP requires the scope resolution operator ($this->). Without it, the code will throw a fatal error regarding an undefined function or method.

The Controller Context

It is important to remember that in CodeIgniter, classes extending Controller (or CI_Controller in modern versions) have access to the framework's super-global variables and helper methods. When you call $this->current_stock() from within $this->add_stock(), both methods share the same instance of the Inventory object.

This means they can access shared properties if you define them at the class level, such as:

class Inventory extends Controller
{
    public $db_handle;
    
    function __construct()
    {
        // Initialize connection once for both methods
        parent::__construct();
        $this->db_handle = $this->load->database('default', TRUE);
    }

    function current_stock()
    {
        return $this->db_handle->query("SELECT * FROM inventory_table");
    }

    function add_stock()
    {
        // Both methods now share the same database connection instance
        $this->current_stock(); 
    }
}

Trade-offs and Design Considerations

While this pattern is technically valid and solves the immediate problem of code reusability within a single class, it introduces specific architectural considerations you should keep in mind.

State Management: If current_stock() modifies global state or relies on external resources that change between calls, calling it sequentially inside another method might lead to race conditions or unexpected behavior, especially if these methods are later moved to asynchronous workers or multi-threaded environments. In a standard PHP script execution flow, this is rarely an issue, but it is worth noting when scaling.

Cyclomatic Complexity: Nesting logic where one method simply delegates to another can sometimes obscure the primary responsibility of the calling function. If add_stock() exists solely to run current_stock(), consider renaming the file or extracting the logic into a separate model or service class. This aligns with the Single Responsibility Principle. However, if you need to chain operations within a specific controller workflow, keeping them in one file is acceptable provided the methods are not too deep.

Avoiding Overkill: The link often cited regarding extending base classes to keep code DRY (Don't Repeat Yourself) suggests creating a parent class with common logic. While that is excellent for large-scale applications where multiple controllers need shared behavior, it is indeed overkill for a simple two-method interaction within one file. Direct invocation via $this keeps your namespace flat and reduces the overhead of inheritance unless you explicitly need to share state across different controller instances.

Modern Syntax Note

If you are updating an existing application or writing new code today, be aware that CodeIgniter 3 used function, but CodeIgniter 4 requires public function. While $this->method_name() works in both versions, the visibility modifiers (public, private, protected) and constructor initialization differ significantly between CI3 and CI4. Ensure your method visibility matches the framework version you are targeting to avoid scope resolution errors.

Our network