从file1到filesA-Z的PHP变量
所以我看到了一些关于用PHP在文件之间传递变量的争议。“最好的方法”是什么 目前,我有一个这样的设置 文件1:从file1到filesA-Z的PHP变量,php,include,Php,Include,所以我看到了一些关于用PHP在文件之间传递变量的争议。“最好的方法”是什么 目前,我有一个这样的设置 文件1: <? $foo = 'somethingCoolHere'; $bar = 'nothingLameThere'; ?> <? include('/link/to/file1.php'); printf('stuff' .$foo. 'stuff'); ?> <? include('/link/to/file1.php'); printf('stuff
<? $foo = 'somethingCoolHere';
$bar = 'nothingLameThere'; ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff'); ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff');
printf('stuff' .$bar. 'stuff'); ?>
<?php
$file1['foo'] = 'somethingCoolHere';
$file1['bar'] = 'nothingLameThere';
?>
<?php
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
?>
<?
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
printf('stuff' .$file1['bar']. 'stuff');
?>
文件A:
<? $foo = 'somethingCoolHere';
$bar = 'nothingLameThere'; ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff'); ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff');
printf('stuff' .$bar. 'stuff'); ?>
<?php
$file1['foo'] = 'somethingCoolHere';
$file1['bar'] = 'nothingLameThere';
?>
<?php
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
?>
<?
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
printf('stuff' .$file1['bar']. 'stuff');
?>
文件B:
<? $foo = 'somethingCoolHere';
$bar = 'nothingLameThere'; ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff'); ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff');
printf('stuff' .$bar. 'stuff'); ?>
<?php
$file1['foo'] = 'somethingCoolHere';
$file1['bar'] = 'nothingLameThere';
?>
<?php
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
?>
<?
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
printf('stuff' .$file1['bar']. 'stuff');
?>
这是“好的”。但是,如果有人想扩展我所拥有的,他们可能需要记住在顶行中包含“include”,以使变量发挥作用。与我现在所做的相比,有“更好的实践”或“最佳实践”的方法吗?就我个人而言(不确定其他人是否同意),在包含文件时,我会使用绝对路径。我会让这些路径保持不变。为什么?因为如果我需要更改路径(比如我移动了一个文件或其他东西),我就不需要遍历每个文件来更改路径,而是只需要定义常量的文件
例如:
constant.php
define('file1' , 'abolute/path/to/file1');
define('fileA' , 'abolute/path/to/fileA');
define('fileB' , 'abolute/path/to/fileB');
然后在需要使用常量的任何文件中包含constant.php:
something.php:
include_once($_SERVER['DOCUMENT_ROOT'].'/constant.php');//we now have access to our constants
include_once(fileA);
include_once(fileB);
include_once(file1);
我并不是说这种方法是最佳实践,但它只是我在所有项目中都会做的事情。我也看不出它有什么问题。我的项目也遵循类似的模式,但我想在您的代码中指出一点。假设多个程序员正在处理一个大型项目,那么很难跟踪在不同文件中定义的变量。因此,我始终确保使用关联数组对相似的值进行分组,而不是对每个值使用单独的变量。你可以考虑使用下面的结构。 文件1:
<? $foo = 'somethingCoolHere';
$bar = 'nothingLameThere'; ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff'); ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff');
printf('stuff' .$bar. 'stuff'); ?>
<?php
$file1['foo'] = 'somethingCoolHere';
$file1['bar'] = 'nothingLameThere';
?>
<?php
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
?>
<?
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
printf('stuff' .$file1['bar']. 'stuff');
?>
文件A:
<? $foo = 'somethingCoolHere';
$bar = 'nothingLameThere'; ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff'); ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff');
printf('stuff' .$bar. 'stuff'); ?>
<?php
$file1['foo'] = 'somethingCoolHere';
$file1['bar'] = 'nothingLameThere';
?>
<?php
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
?>
<?
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
printf('stuff' .$file1['bar']. 'stuff');
?>
文件B:
<? $foo = 'somethingCoolHere';
$bar = 'nothingLameThere'; ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff'); ?>
<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff');
printf('stuff' .$bar. 'stuff'); ?>
<?php
$file1['foo'] = 'somethingCoolHere';
$file1['bar'] = 'nothingLameThere';
?>
<?php
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
?>
<?
include('file1.php');
printf('stuff' .$file1['foo']. 'stuff');
printf('stuff' .$file1['bar']. 'stuff');
?>
通过这种方式,您可以组织数据,而且可以减少代码审查和后期开发的难度。不,仅此而已。这就是MediaWiki和其他东西的web配置文件的工作方式。如果它没有坏,不要修复它。如果有人拿走了我得到的代码并想向其中添加文件,他们应该足够聪明地添加
include('/link/to/file1.php')代码>到顶部?是的。这就是它应该如何工作的。这太一般化了,无法得到你想要的,但是你所做的没有错,除了短标记我实际上使用的是完整标记我使用的是绝对路径。虽然我甚至没有想到“define”函数。这将有助于清理我的一些代码。因此,为了扩展以改进我的现有资源,我接受以下内容:)