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:

/root/index.php

root/includes/class.inc.php
root/includes/config.inc.php

So, the index includes the class, and the class includes the config.inc.php.

However, am I right in thinking, that for the class to include the config.inc.php, it will start the path from where *it* was originally called? i.e if my class file simply has include('config.inc.php'); 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?
andycalAsked:
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.

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

include("includes/config.inc.php");

Cheers
0
hernst42Commented:
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

config.php
<?php

// make settings
....

//include classes and function

require_once 'includes/class.inc.php';
require_once 'includes/class2.inc.php';
....

on the pages i use the only do a
require_once 'config.php';
0
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.
0
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.

nicholassolutionsCommented:
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():
http://us2.php.net/manual/en/function.ini-set.php

For information on include_path:
http://us2.php.net/manual/en/ini.sect.path-directory.php#ini.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/.

Cheers,
Matt
0
gruntarCommented:
To this you just have to set up include path, where your includes reside like this
include_path=".:/root/includes"

then you include those files just like if they were in root
include('config.inc.php');
That way your include will be the same no matter in what direcory file is.

Cheers
0
RoonaanCommented:
You can use the statement below in your class.inc.php to always include the config correctly, even when you are not sure about the original basepath:

include_once(dirname(__FILE__).'/config.inc.php');

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

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

Regards

-r-
0

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.