php正确添加所需文件

i have following folder structure.

enter image description here

config.php

<?php
class config {
    const server = "localhost";
}

conn.php

<?php
 require_once './config.php';

firstExample.php

<?php

require_once './db/conn.php';

when i run conn.php there is no errors.but if i run firstExample.php i got following error

Warning: require_once(./config.php): failed to open stream: No such file or directory in C:\wamp\www\testX\db\conn.php on line 2

i do some tests and able to fix the error by changing conn.php to

require_once './db/config.php';

my question is i need to add conn.php from lot of folders,sub folders.so changing like above doesn't work.for example if i run conn.php after above change then again i'm getting same error.

what i want to know is correct way of adding files.so when i add conn.php to any file config.php should be included.

Look at Directory structure

you do not need to put "./"

Try:

 require_once 'config.php';

Or you can define path in CONSTANT and use it.

You can use $_SERVER variable in PHP : $_SERVER['DOCUMENT_ROOT']

So your conn.php will change to:

<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/db/config.php';

when testX is your root folder.

You can use $_SERVER['DOCUMENT_ROOT']. This is path of your document root C:\wamp\www, so including despite where the file is the link to required file is the same require_once $_SERVER['DOCUMENT_ROOT'].'/testX/db/config.php';

But due to server settings it not always shows to this directory. For more bullet proof method you should define path in your index.php or other file, that is in root directory & later use that define, eg: define('BASE_PATH', dirname(__FILE__) ); and later just use:
require_once BASE_PATH.'/testX/db/config.php;

You'll want to use the magic constant __DIR__ which will build absolute path based on the directory containing the file being executed/included:

conn.php

require_once __DIR__.'/config.php';

firstExample.php

require_once __DIR__.'/db/conn.php';

Source: http://php.net/manual/en/language.constants.predefined.php