I am working with PHP and I have come up with a problem that I have been looking for and I have not been able to solve. The problem comes when in a class I try to require another class in this way:
require 'DAO/usuario.php';
But when it reaches these lines it gives an error:
require '../../compra.php'
The error is the following:
require (../../compra.php): failed to open stream: No such file or directory
But the route exists, and it is. Does anyone know why it gives this error with that route and knows how to solve it. Thanks.
You're using a relative path, and depending on the
requires
e stringincludes
you've defined, what's two directories aboveDAO/usuario.php
isn't two directories above the initial script.Let's say you have it like this
It would be understood that it
compra.php
is in the same directory asindex.php
, but it turns out thatusuario.php
it is running at compile time along withindex.php
, so you are calling a file two directories aboveindex.php
and that clearly doesn't exist.What I always recommend is to keep a single entry point to the application (index.php) where a constant is defined that sets the root directory:
index.php
And in user.php
And the real best practice here is to use Composer and autoloading , so that the namespace of each class deterministically defines its location in the file tree, but sometimes it doesn't need to get that complicated if you're experimenting or doing a proof of concept.