PHP include paths

More of a general one this. I've got a bunch of classes that I use day to day. I've been playing with one of them, and I have tried to include another file at the top of the class containing some configuration options.

My path is like this:



So, the index includes the class, and the class includes the

However, am I right in thinking, that for the class to include the, it will start the path from where *it* was originally called? i.e if my class file simply has include(''); it will in fact try to call it in from the root directory, not where the class itself is stored?

What's the most flexible way around this?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you can include a file that is not in a root dir where index.php is like this


if you want to include a file once use
require_once or include_once, so such a php-file is only included once, even if its called with require_once multiple times.

I typical have a fill which includes all nexessary thing e.g


// make settings

//include classes and function

require_once 'includes/';
require_once 'includes/';

on the pages i use the only do a
require_once 'config.php';
andycalAuthor Commented:
That's kinda relying on knowing always the path, I guess what I'm after is a way of ensuring that the config file is called into the class file, always using the class file path.
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

you can also set your include_path directive in the php ini file such so that PHP searches for included files in the way that you want. You can do this in your script at runtime using ini_set():

For information on include_path:

From the PHP manual:
Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/ and then in /www/include/.

To this you just have to set up include path, where your includes reside like this

then you include those files just like if they were in root
That way your include will be the same no matter in what direcory file is.

You can use the statement below in your to always include the config correctly, even when you are not sure about the original basepath:


__FILE__ contains the actual absolute path to /root/includes/, therefor dirname(__FILE__) returns /root/includes.

Even when you move the files, but they both stay in the same directory, this method still works.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.