Solution:1
Effectively, you don’t have the right path and include
will allow you to continue if the file doesn’t exist.
When including or requiring a file, if the path you supply starts with a /
or \
then PHP will treat it as a path from the root of the current filesystem. When you supply a path that doesn’t start with one of those, PHP thinks it is a relative path it will try to guess which file to include based on where the current file is and other directories it knows about.
To fix you will likely want to do the following:
require_once __DIR__.'/folder/class.my-class.php';
See the docs on include
, include_once
, and as well as __DIR__
.
Recommendation:
Whenever including a file you should try to use require_once
whenever possible. If it is a file that you know can be included multiple times then you may use require
. If it is a file that is OK to be omitted if it for whatever reason doesn’t exist, then you may use include_once
. If the file can be both, only then should you use include
.
However, as an experienced programmer I can also tell you that if you are using either include_once
or include
you are doing something wrong and should be checking if a files exists before trying to blindly include it.
Also, I highly recommend having the below code active at all times. This will help you catch breaking errors before they have a chance to actually break. Or at least grant you a better understanding of why something broke.
ini_set('display_errors', '1');
error_reporting(-1);