PHP: cannot declare class because the name is already in use
When you attempt to include somescript.php which loads both child1.php and child2.php, PHP throws a fatal error regarding the Database class. The specific message indicates that the class name is already in use within database.php. While loading just one child script works perfectly, combining them breaks the application because of how PHP handles file inclusion and global state.
The root cause lies in the execution order and the presence of duplicate definitions. When somescript.php runs, it likely includes or requires both children. If child1.php is processed first, it executes its include 'parent.php' statement. This triggers the loading of database.php, defining the Database class for the first time. Subsequently, when child2.php is included, it also runs include 'parent.php'. Since parent.php contains include 'database.php', PHP attempts to define the Database class again in the same global scope. In PHP, classes are singletons within a process; you cannot redefine them unless specific conditions are met. This results in the "cannot declare class" fatal error.
To resolve this, you must ensure that database.php is included only once per request lifecycle, regardless of how many times it is requested by child classes. The standard solution involves using an autoloader or a dedicated initialization file to manage dependencies centrally.
Instead of having every script include database.php directly, create a central bootstrap file, such as app/bootstrap.php. This file should load the database definition and then provide a method for other files to initialize their own classes. You can structure parent.php to check if the parent class already exists before defining it:
// app/bootstrap.php
require_once __DIR__ . '/database.php';
if (!class_exists('Parent')) {
// Define Parent here only if it hasn't been loaded yet
// Or better yet, move the Parent definition to a separate file
}
A more robust approach is to decouple the class definitions from the inclusion logic. Move the Database class into its own namespace or use an autoloader like Composer. With Composer, you define the dependencies in your composer.json, and the autoloader ensures that database.php is loaded exactly once when any of its classes are instantiated. This prevents duplicate includes entirely.
Alternatively, if you must stick to manual includes without Composer, implement a simple check in parent.php or a wrapper function. However, simply adding if (!class_exists('Database')) inside database.php is not the right pattern for fixing the include chain; that file should define the class unconditionally. The fix belongs in how you orchestrate the includes.
Consider refactoring somescript.php to load dependencies explicitly once:
// somescript.php
require_once 'app/database.php'; // Load database once at the top
include 'child1.php';
include 'child2.php';
$child1 = new Child1();
$child2 = new Child2();
By moving the require_once 'database.php' to the very beginning of your entry point, you guarantee that the Database class is defined in memory before any child scripts attempt to load it. When child1.php and child2.php subsequently include parent.php, the check inside parent.php (or simply the fact that the class already exists) prevents the duplicate definition error.
Another valid pattern is to ensure that parent.php does not blindly include database.php. Instead, pass the database instance or let the child scripts handle the dependency injection if they are using a framework like Laravel. In modern PHP development, relying on frameworks for dependency management is often preferred over manual file inclusion chains. For instance, integrating with https://laravelcompany.com resources can provide insights into how Laravel handles service providers and facades to avoid such namespace collisions automatically.
Ultimately, the goal is to treat class definitions as singletons that should be instantiated or checked for existence before re-definition. By centralizing the inclusion of database.php in your entry script or utilizing an autoloader, you eliminate the ambiguity of duplicate includes and restore stability to your application flow.