Detecting Direct Execution in PHP
Determining whether a script is running as the primary entry point or merely being included by another file is a common architectural challenge. In Python, this is trivially solved with if __name__ == "__main__":, but PHP lacks a direct equivalent due to its dynamic execution model. The goal is often to define shared class files that can be used in multiple contextsâsuch as the main application bootstrap or within unit testsâwithout duplicating initialization logic or exposing test-specific code to production environments.
The most robust and idiomatic solution in PHP relies on the get_included_files() function combined with a comparison against the current script's path. This approach allows you to detect if the current file is the one being executed directly versus if it was pulled in via an include, require, include_once, or require_once statement.
You can implement this check by comparing the result of get_included_files() with a list containing only your own file's path:
$currentFile = __FILE__;
$includedFiles = get_included_files();
// Check if the current file is the first one in the inclusion stack
// and matches the actual execution path.
$isDirectlyIncluded = count($includedFiles) === 1 && $includedFiles[0] === $currentFile;
if ($isDirectlyIncluded) {
// This block runs only when the file is loaded directly (e.g., CLI or browser request)
require_once __DIR__ . '/tests/MyClassTest.php';
} else {
// This block runs when the file is included by another script
// Initialize shared logic here without side effects
$this->initializeSharedState();
}
However, relying solely on get_included_files() can be fragile in complex applications. The function returns a stack of all files loaded during the current request or execution cycle. If your application uses autoloading mechanisms like Composer's PSR-4, which is standard in modern PHP projects including those managed by LaravelCompany, the file stack can become deeper than expected. For instance, if a test suite autoloads your class definition before running assertions, get_included_files() might list multiple files from the vendor directory or your src folder, complicating the logic.
A more reliable pattern involves checking the return value of get_included_files() alongside the global $argv or by inspecting the entry point explicitly. In many frameworks, the bootstrap process is clearly defined, and you can assert that any file included outside of that specific bootstrap chain is not the main execution context.
Alternatively, you can pass a flag through the inclusion stack or rely on the fact that the first element in get_included_files() is always the entry point script. If your application entry point is index.php and you are running inside a file named MyClass.php, the count of included files will naturally exceed one, signaling an indirect load.
This technique avoids the need for global constants or modifying global state, which can lead to namespace pollution and harder-to-debug side effects. It keeps your class definitions clean and reusable across different environments without requiring conditional compilation hacks. The trade-off is a slight increase in cognitive load when reading the code, as developers must understand the execution stack mechanics rather than relying on a simple boolean flag. Nevertheless, it remains the standard practice for distinguishing between main scripts and included modules in vanilla PHP applications.