I’m building a web app that has php files spread out through different directories, so a typical file may have several includes. All these files everywhere.
a php file may look like this:
include('../../some.php');
require('../../some.php');
and in another file you may have something like this:
include('../../../../some.php');
require('../../../../some.php');
point being, this can get a little bit out of hand, and somewhat difficult to keep track of. Is there a way I can do this so that I don’t need to ../../.. all the time?
I tried doing this:
include('http://www.mywebsite.com/some.php');
require('http://www.mywebsite.com/some.php');
but it doesn’t seem to work, but what’s funny is that I won’t get a PHP error when I use require, only a
call to function x() error, function or object doesn't exist type error. It never said anything about the file not existing. Thank you in advance.
Do not trust
$_SERVERvariables. In some environments they can be set/altered by the user making the request. It’s much better to define the base path manually in your index/bootstrap file and use it when needed.or on version of PHP before 5.3 you can do this
Now you can always know the path to your files.
Both
__DIR__and__FILE__are safe constants set by PHP and can be trusted.You can autoload classes like this:
In other news, I can’t ever think of a good use for
include(). If you need to use include you are doing something wrong.