php include paths

nonis

n00b
Joined
Jun 22, 2009
Messages
29
I have a question about php include paths.

I have a site that looks like this:
site/index.php
site/sub/config.php
site/sub/functions.php

where config holds configuration variables and functions holds some reusable functions.

config:
Code:
<?php $v = "a variable"; ?>
functions:
Code:
<?php
include "./config.php";
echo "v outside function = " . $v . "\n";

function func()
{
echo "v inside function = " . $v . "\n";
}

func();

?>
index:
Code:
<?php
include "./sub/functions.php";

echo "v in index = " . $v . "\n";

func();
?>
I have a couple of problems. One is that if I run functions.php, the variable is correct outside the function, but null inside. Why is that? Do I have to include config.php inside the function and outside? Do variables lose their scope moving into a function in php?

The second is that if I run index.php, all variables are null. It seems that if I change the include in functions to "./sub/config.php" then it works (other than the above problem). But this breaks functions.php running on it's own. Is there a solution to this or do I have to have one or the other?
 
Just a warning: global variables are bad!

To answer your question:
Variables have local scope inside a function. You have to add a global keyword so inside each function that uses a global variable:
PHP:
function aFunction() {
  global $v;
  echo $v;
}

You can try including files like this:
PHP:
// in functions.php
include dirname(__FILE__).'/config.php';

// in index.php
include dirname(__FILE__).'/sub/functions.php';

I think __FILE__ refers to the included file rather than the running file.

(none of this was tested...)
 
first, you can't see the variable in the function since it is a global variable. try
Code:
function func() {
    global $v;
    echo "v inside function $v\n";
}
see http://us3.php.net/manual/en/language.variables.scope.php

the debate about whether you should use global variables is something i won't derail this thread with

2nd, include and require paths are relative to the currently executing script, not whatever file they are being invoked from. when you run index which then includes functions, index is still the current script regardless of where the code is included from. the simple solution is something like this in your functions
Code:
include dirname(__FILE__) . "/config.php";
you can do that anywhere to specify a path relative to the current file that the code is in, as opposed to the currently executing file. you can use something like
include dirname(__FILE__) . "/../../two_levels_up.php"
for a file in a different directory

something to consider is whether you want to use require instead of include (it generates an error if for some reason it can't load the include file. generally i use require since i generally really need the file to load and there's not much point to continue the rest of the script if it couldn't load.

also you might consider using require_once (or include_once) instead of just require/include. if your functions include the config and you have another page include the config by accident then your config values would get reset, which may or may not have an effect but would probably be unwanted regardless.
 
Thanks a lot to both of you! I come from a C++ background and I'm used to the #ifndef, #define, and #endif so I will probably end up using require_once.

What is the best solution instead of global variables? This example was constructed to make a point. In reality I am creating an image blog software which stores the hostname, user, and password for mysql in a config.php file, has some functions for connecting to, accessing, and disconnecting from the mysql db/table in functions.php, and index.php is a page which displays things using those functions. I based the config.php idea off of phpMyAdmin which seems to be a legit software and also stores some globally used variables in a config.php file somewhere I believe.

Thanks!
 
One way is to enclose it in a class and use getter methods to make them read only.
Code:
class Config {
  private $user = 'user';
  private $password = 'password';

  public function __get($var) {
    if(property_exists($this, $var)) {
      return $this->$var;
    }
    else {
      throw new Exception('an error');
    }
  }
}

$config = new Config;
echo $config->user;
 
There are classes in PHP?! I haven't used PHP since I was like 13, didn't even know about classes then. Thanks.
 
Ok... last question. I promise. [=

I am now storing my globally-used stuff in a class. But the following does not work, why?
Code:
class Config {
 private $imgpath = dirname(__FILE__) . "/images/";
}

Is it __FILE__ or dirname() or both that doesn't work within the class? How do I get them in there?
 
see http://www.php.net/manual/en/language.oop5.properties.php

when you initialize a class variable the expression must be able to be evaluated at 'compile' time (quotes only for depending how you consider php since it's interpreted and whatnot unless you're using an optimizer dealie or something). i presume the compiler doesn't know anything about the magic variables (i.e. __FILE__) at 'compile' time.

what you could try instead would be to initialize the value in the constructor
Code:
class Config {
    private $imgpath;

    function __construct() {
        $this->imgpath = dirname(__FILE__) . "/images/";
    }
}
 
Back
Top