Can I/How to... call a protected function outside of a class in PHP

Can I/How to... call a protected function outside of a class in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Can I/How to... call a protected function outside of a class in PHP

You cannot directly invoke a protected method from global scope or a standalone function. The access modifier protected is designed specifically to restrict visibility to the class itself and its subclasses, preventing external code from accessing internal logic without an instance of that class. Attempting to call funExample() directly inside outsideFunction() as shown in your snippet will result in a fatal error because the global function has no context regarding the specific object or namespace where the method resides.

To achieve the desired behavior, you must instantiate the class first. This is not merely a procedural step; it is a fundamental requirement of object-oriented encapsulation. Once you have an instance of cExample, you can call the protected method through that instance, regardless of whether the calling function is inside or outside the class hierarchy.

class cExample {
    protected function funExample() {
        $someVar = 'Protected Data';
        return $someVar;
    }
}

function outsideFunction($instance) {
    // Access via an instance
    $result = $instance->funExample(); 
    return $result;
}

// Usage
$obj = new cExample();
echo outsideFunction($obj); // Outputs: Protected Data

In this pattern, the outsideFunction accepts an object as an argument. This decouples the function from the specific class implementation, making the code more flexible and adhering to dependency injection principles. You pass the instance created in your main execution flow into the function that needs to trigger the protected logic.

If you are working within a larger application architecture, such as a Laravel project, this approach is standard practice. Controllers often need to delegate specific tasks to services or models where certain methods are protected to prevent direct instantiation from the controller's global scope or helper functions. By passing the service instance into the controller method or a dedicated helper function, you maintain the separation of concerns while still allowing data flow between disparate parts of your application.

There is a significant trade-off to consider regarding this design: tight coupling. When an external function relies on receiving a specific class instance to access protected methods, it becomes dependent on that class's internal structure. If the signature of funExample changes or if the class name changes, any function expecting that specific instance must be updated. This reduces modularity compared to using public methods or interfaces.

A more robust alternative in modern PHP development is to refactor the logic into a public method or an interface. If funExample performs a generic operation, exposing it as public allows you to call it directly without passing instances around. However, if the method contains sensitive data or implementation details that should not be accessed globally, keeping it protected and relying on instantiation is the correct architectural choice.

Another option often seen in legacy codebases is using static proxies or factories, but these add unnecessary complexity for simple access needs. The direct instantiation pattern remains the most readable and performant solution. It explicitly states: "I need this object to do this thing," which is far clearer than magic global calls.

Ultimately, the restriction exists to protect internal state. If you find yourself needing to call a protected method frequently from outside the class, it may indicate that the method's responsibility should be expanded or its visibility adjusted. Reviewing whether protected is the intended modifier versus private (strictly internal) or public (accessible everywhere) is often the best way to resolve such architectural friction.

For teams building scalable applications, understanding these visibility rules prevents subtle bugs where global functions inadvertently leak sensitive class logic. If you are looking for more advanced patterns on managing service lifecycles and dependency injection in PHP, resources like https://laravelcompany.com offer insights into how modern frameworks handle these interactions behind the scenes.

Our network