简体   繁体   中英

Include php file that includes another php file?

My PhP files contain some long string constants and I'm trying to factor them out. So I created "my_string_constants.php" and used include in header.php . So far that works fine.

Now another file, page.php also requires the string constants and header.php . The scheme below tries to clarify these dependendies.

在此处输入图片说明

The string constants now seem available in my header only, not the rest of my page. I tried to resolve this by adding global ... to each string constant in string_constants.php . This resolved the error of "Unknown variable" but my string constants still seem unavailable to most of the page content.

What's the right way to get this working?

UPDATE

The issue's been solved . I should have used define(myString instead of $myString = ... . By doing so, I need just one include in header.php and the constants will be available to page.php as well.

Thanks a million, you guys are great.

One thing you would want to do is distinguish a constant from a variable. Especially if other developers end up working on this, they will be confused by your terminology.

For constants, you do not need to declare them as global and they are defined like so:

define('MY_CONSTANT', 'Value');

It seems to me that the constants file is acting as your site wide configuration file, so to me, it makes sense to have that on every page, regardless of whether header is used or not. I would normally create a bootstrap file for this purpose.

bootstrap.php:

<?php
require_once(__DIR__ . '/constants.php');
require_once(__DIR__ . '/database.php');
require_once(__DIR__ . '/session.php');

You get the point, then every accessible page needs to include this bootstrap file and then possibly the header.

page.php:

<?php
require_once(__DIR__ . '/bootstrap/bootstrap.php');
require_once(__DIR__ . '/header.php');
?>
<h1>Page Title</h1>

In header.php, since it requires these constants, you can handle this in two ways, either check that the constants are defined (meaning, bootstrap was included first) or just use another require_once to make sure that file was loaded.

if (!defined('MY_CONSTANT')) exit('Bootstrap failure');

or

require_once(__DIR__ . '/bootstrap/constants.php');

Going to many directories or "files" deep regarding includes can really cause issues later when you are trying to debug. As a rule I try to only go one level deep in regards to including files. Ie Create a folder called includes and place everything in there. If there is a file that needs multiple variables, functions etc, then include them in the needed pages at that point doing several includes like so:

<?php 
include("includes/header.php"); 
includes("includes/functions.php");
?>

There are also other issues in regards to having multiple includes, like if you have sessions or cookies some LAMP stacks will require you to declare

session_start();

at the top of every page including all included php files that may need access to that session or cookie.

So to answer your question I believe the simplest solution would be to re-organize your site or script.

in the header page ontop u write
include 'header.php';

and in header.php you write include 'my_string_constants.php';

so in this case the page.php calls the header and in the header the my_string_constants is being called...is this what you mean?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM