require_once :failed to open stream: no such file or directory
You are encountering a classic relative pathing issue where the logic of your file inclusion breaks down because it depends on the absolute filesystem location of the script, rather than its position within your project hierarchy. When you execute require_once('../mysite/php/classes/eventManager.php') from "PAGE A", PHP resolves that path relative to the current working directory (CWD). If PAGE A lives at C:\wamp\www\mysite, PHP looks for the file at C:\wamp\includes.
The real problem, however, hides inside eventManager.php. Once PHP successfully loads that file, it immediately hits the second line of code: require_once('../includes/dbconn.inc');. At this specific moment, the CWD is C:\wamp\www\mysite\php\classes. The relative path ../includes tells PHP to look one directory up (landing in C:\wamp\www\mysite\php) and then into an includes folder. Since your folder structure places the database connection file at C:\wamp\www\mysite\includes, that specific lookup fails with the "No such file or directory" error you are seeing.
The core architectural flaw here is the inconsistent use of relative paths across different layers of your application. Your top-level script assumes it is running from the project root (C:\wamp\www\mysite), but your internal class files assume they are being executed from a subdirectory (C:\wamp\www\mysite\php\classes). This creates a fragile dependency chain where moving a single file can break the entire include graph.
To resolve this, you must normalize your paths so that they resolve correctly regardless of which script initiates the load. The most robust solution is to use dirname(__FILE__) or, preferably in modern PHP versions, __DIR__. These constants represent the directory containing the current file, allowing you to construct paths relative to the file's actual location rather than the server's current working directory.
Here is how you should refactor eventManager.php:
<?php
// Define a base path based on where this file lives
$basePath = __DIR__ . '/../../includes';
require_once($basePath . '/dbconn.inc');
class EventManager { ... }
?>
By calculating $basePath, you ensure that regardless of whether eventManager.php is called from the root, a subfolder, or via a web server's CWD, it will correctly resolve to C:\wamp\www\mysite\includes. This pattern isolates the file structure logic within the file itself, preventing external scripts from needing to know your internal layout.
Alternatively, you could refactor the top-level include in "PAGE A" to point directly to the target without relying on a relative jump that assumes a specific CWD:
// Assuming PAGE A is at /mysite/index.php
require_once(__DIR__ . '/../mysite/php/classes/eventManager.php');
Using __DIR__ (or dirname(__FILE__) in older PHP versions) eliminates the ambiguity of relative paths. It shifts the burden of path resolution to the file itself, making your application portable across different server environments or directory structures without needing to reconfigure every include statement.
While some developers attempt to use getcwd() to manually adjust the working directory before including files, this is generally discouraged in modern PHP development because it can lead to side effects and makes debugging significantly harder. Sticking to absolute paths derived from __DIR__ is the industry standard for maintaining stability. If you are building larger applications, consider moving towards dependency injection or autoloading mechanisms that handle these path calculations internally, ensuring your core logic remains decoupled from filesystem navigation.
