PHP includes confusion

I am creating a basic structure for my site and I have a config.php stored in the scr directory. In addition, in this directory there is a folder called php , where I store a file called sidebar.php

Basically, the config.php will contain my database configuration variables and is also the only file that every page invokes. I want to include php/sidebar.php in this file so that any variables that I create in sidebar.php can be used on all pages.

However, I also want sidebar.php have access to database configuration variables. Thus, these two files will effectively include each other.

Enable from configuration on sidebar:

 include 'php/sidebar.php'; 

From sidebar to configuration:

 include '../config.php'; 

However, the above statement (sidebar to config) gives the following error message:

Warning: enable (../config.php) [function.include]: failed to open Stream:

There is no such file or directory in C: \ XAMPP \ site \ ecr \ PHP \ sidebar.php on line 3

I'm going to all this incorrectly cross-link two files, or is there a decent reason why it doesn't work

+4
source share
5 answers

I think the php parser is already reading the file. You should use include_once instead of include to avoid this problem.

In addition, you should also seriously consider require_once , since it protects against the case when the included file does not work, t exists, especially for the configuration file, your application should work, and not ignore it (And this is what is required).

PS : like others, cyclic dependency must be broken anyway, this is a pretty good sign of poor design.

+3
source

In general, a safe way to include is to use dirname(__FILE__) or __DIR__ , if you use PHP> = 5.3 to have an absolute path - it is written relative to the current one.

For example, you can use:

 include dirname(__FILE__) . '/php/sidebar.php'; 


Note: dirname(__FILE__) and __DIR__ point to the directory of the file in which they are written.

You don’t have to worry about what is included in it and includes: you always refer to files using absolute pats.


Relevant pages in the manual:


Also, as pointed out by @ jwir3 in your comment, you sometimes want to use require_once() or include_once() to make sure that the given file is not included more than once.

Including a file more than once can lead to problems, especially if it contains constants, functions, or classes.

+6
source

You need to enable the sidebar from the config, and not vice versa:

 // in config.php include('php/sidebar.php'); // in other files, such as page.php include ('config.php'); 
+2
source

If you enable the sidebar inside the configuration, it should have access to any variables in the global area. However, you may need the global $ varName inside the function.

Include a configuration on each page, enable a sidebar inside the configuration, and access configuration variables from the sidebar. Do not worry about double inclusion.

0
source

So you have

 /path/to/your/site/scr/config.php /path/to/your/site/php/sidebar.php 

and you want sidebar.php to load config.php?

 include('../scr/config.php'); 

will do the trick.

0
source

Source: https://habr.com/ru/post/1344669/


All Articles