Calling other function in the same controller?

Calling other function in the same controller?

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

Stefan Bogdanescu

Founder & Senior Architect

Calling Other Functions Within the Same Controller Class

In PHP, a common stumbling block for developers transitioning from simple scripts to structured frameworks is understanding function scope. The specific error Call to undefined function sendRequest() occurs because you are referencing a method that exists within your current class instance as if it were a global standalone function. This distinction is critical in object-oriented programming (OOP).

When you define a method inside a class, such as sendRequest, it becomes a member of that specific class. The PHP engine does not automatically promote these members to the global namespace for use outside the class context. Consequently, when your read method attempts to call sendRequest($uri) without any prefix, the engine searches the global scope first. Finding nothing there, it throws the "undefined function" error.

To resolve this, you must explicitly tell PHP which class owns the method you intend to invoke. In your InstagramController example, the fix is straightforward: prepend the class name to the method call. Since the read method resides within the InstagramController class, you should reference the static method using self::sendRequest() or static::sendRequest().

Here is how the corrected read method looks:

public function read($q)
{
    $client_id = 'ea7bee895ef34ed08eacad639f515897';

    $uri = 'https://api.instagram.com/v1/tags/'.$q.'/media/recent?client_id='.$client_id;
    
    // Use self:: or static:: to reference the method within the same class
    return self::sendRequest($uri);
}

While this syntax works perfectly fine, it is worth noting that relying on global function references (calling function_name() without a prefix) is generally an anti-pattern in OOP. It couples your code tightly to the global scope and makes refactoring difficult. For instance, if you ever decide to extract the logic into a separate service class named InstagramService, you would have to find every instance of sendRequest globally and update it. By always using self::, you ensure that the method resolution is encapsulated within the class hierarchy.

Another nuance involves the distinction between self and static. Using self::sendRequest() refers to the method in the current class definition, while static::sendRequest() utilizes late static binding. In this specific scenario, since sendRequest has no arguments and does not depend on any overridden methods in child classes, both approaches yield identical results. However, adhering to a consistent style—preferably using self:: for internal class calls—is a best practice that prevents confusion when inheritance hierarchies are introduced later.

If you find yourself frequently needing to call helper functions that currently live inside your controller, it is often a sign that those helpers belong in a dedicated service class or a trait. Controllers should remain focused on handling HTTP requests and responses, delegating data fetching logic to services. This separation of concerns keeps your controllers lean and testable.

While frameworks like Laravel provide robust tools for handling external API calls via the Http facade or the Request object, understanding these fundamental PHP scoping rules is essential before relying on framework-specific helpers. If you are building a custom solution outside of a full framework stack, ensuring that your utility functions are explicitly scoped to their parent class prevents these runtime errors entirely.

Our network