简体   繁体   English

如何在 php 中设置站点范围的变量?

[英]How to setup site-wide variables in php?

I want to define something like this in php :我想在php 中定义这样的东西:

$EL = "\n<br />\n";

and then use that variable as an "endline" marker all over my site, like this:然后在我的网站上使用该变量作为“结束线”标记,如下所示:

echo "Blah blah blah{$EL}";

How do I define $EL once (in only 1 file), include it on every page on my site, and not have to reference it using the (strangely backwards) global $EL;我如何定义 $EL 一次(仅在 1 个文件中),将其包含在我网站的每个页面上,而不必使用(奇怪地向后) global $EL;引用它global $EL; statement in every page function?每个页面函数中的语句?

Most PHP sites should have a file (I call it a header) that you include on every single page of the site.大多数 PHP 站点都应该有一个文件(我称之为标题),您可以将其包含在站点的每个页面上。 If you put that first line of code in the header file, then include it like this on every page:如果你把第一行代码放在头文件中,那么在每一页上都像这样包含它:

 include 'header.php';

you won't have to use the global keyword or anything, the second line of code you wrote should work.您不必使用 global 关键字或任何东西,您编写的第二行代码应该可以工作。

Edit: Oh sorry, that won't work inside functions... now I see your problem.编辑:哦,抱歉,这在函数内部不起作用......现在我看到了你的问题。

Edit #2: Ok, take my original advice with the header, but use a define() rather than a variable.编辑 #2:好的,将我的原始建议与标题一起使用,但使用define()而不是变量。 Those work inside functions after being included.这些在被包含后在函数内部工作。

Sounds like the job of a constant.听起来像是一个常数的工作。 See the function define() .请参阅函数define()

Do this define ('el','\\n\\<\\br/>\\n');这样做define ('el','\\n\\<\\br/>\\n'); save it as el.php将其另存为 el.php

then you can include any files you want to use, ie然后你可以包含任何你想使用的文件,即

echo 'something'.el;回声'东西'.el; // note I just add el at end of line or in front // 注意我只是在行尾或前面添加了 el

Hope this help希望这有帮助

NOTE please remove the '\\' after < br since I had to put it in or it wont show br tag on the answer...请注意,请在 < br 之后删除“\\”,因为我必须将其放入,否则它不会在答案上显示 br 标签...

Are you using PHP5?你用的是PHP5吗? If you define the __autoload() function and use a class with some constants, you can call them where you need them.如果您定义 __autoload() 函数并使用带有一些常量的类,则可以在需要的地方调用它们。 The only aggravating thing about this is that you have to type something a little longer, like唯一令人恼火的是,您必须输入更长的时间,例如

MyClass::MY_CONST

The benefit is that if you ever decide to change the way that you handle new lines, you only have to change it in one place.好处是,如果您决定更改处理新行的方式,只需在一处更改即可。

Of course, a possible negative is that you're calling including an extra function (__autoload()), running that function (when you reference the class), which then loads another file (your class file).当然,一个可能的负面因素是您调用包含一个额外的函数 (__autoload()),运行该函数(当您引用类时),然后加载另一个文件(您的类文件)。 That might be more overhead than it's worth.这可能比它的价值更多的开销。

If I may offer a suggestion, it would be avoiding this sort of echoing that requires echoing tags (like <br /> ).如果我可以提供建议,它将避免这种需要回显标签(如<br /> )的回显。 If you could set up something a little more template-esque, you could handle the nl's without having to explicitly type them.如果您可以设置一些更像模板的东西,您就可以处理 nl,而无需显式键入它们。 So instead of所以代替

echo "Blah Blah Blah\n<br />\n";

try:尝试:

<?php
if($condition) {
?>
<p>Blah blah blah
<br />
</p>
<?php
}
?>

It just seems to me like calling up classes or including variables within functions as well as out is a lot of work that doesn't need to be done, and, if at all possible, those sorts of situations are best avoided.在我看来,调用类或在函数中包含变量以及 out 是很多不需要完成的工作,并且,如果可能的话,最好避免这些情况。

@svec yes this will, you just have to include the file inside the function also. @svec 是的,这将,您只需将文件也包含在函数中。 This is how most of my software works.这就是我的大多数软件的工作方式。

function myFunc()
 {
require 'config.php';
//Variables from config are available now.
 }

Another option is to use an object with public static properties.另一种选择是使用具有公共静态属性的对象。 I used to use $GLOBALS but most editors don't auto complete $GLOBALS.我曾经使用 $GLOBALS 但大多数编辑器不会自动完成 $GLOBALS。 Also, un-instantiated classes are available everywhere (because you can instatiate everywhere without telling PHP you are going to use the class).此外,未实例化的类随处可用(因为您可以在任何地方实例化,而无需告诉 PHP 您将要使用该类)。 Example:例子:

<?php

class SITE {
    public static $el;
}

SITE::$el = "\n<br />\n";

function Test() {
    echo SITE::$el;
}

Test();

?>

This will output <br />这将输出<br />

This is also easier to deal with than costants as you can put any type of value within the property (array, string, int, etc) whereas constants cannot contain arrays.这也比 costants 更容易处理,因为您可以在属性(数组、字符串、整数等)中放置任何类型的值,而常量不能包含数组。

This was suggested to my by a user on the PhpEd forums .这是PhpEd 论坛上的一位用户向我建议的。

svec, use a PHP framework. svec,使用PHP框架。 Just any - there's plenty of them out there.随便 - 那里有很多。 This is the right way to do it.这是正确的方法。 With framework you have single entry point for your application, so defining site-wide variables is easy and natural.使用框架,您的应用程序具有单一入口点,因此定义站点范围的变量既简单又自然。 Also you don't need to care about including header files nor checking if user is logged in on every page - decent framework will do it for you.此外,您不需要关心包含头文件或检查用户是否在每个页面上登录 - 体面的框架会为您完成。

See:看:

Invest some time in learning one of them and it will pay back very soon.花一些时间学习其中之一,它很快就会得到回报。

You can use the auto_prepend_file directive to pre parse a file.您可以使用auto_prepend_file 指令来预解析文件。 Add the directive to your configuration, and point it to a file in your include path.将该指令添加到您的配置中,并将其指向包含路径中的文件。 In that file add your constants, global variables, functions or whatever you like.在该文件中添加您的常量、全局变量、函数或任何您喜欢的内容。

So if your prepend file contains:因此,如果您的前置文件包含:

<?php
define('FOO', 'badger');

In another Php file you could access the constant:在另一个 PHP 文件中,您可以访问常量:

echo 'this is my '. FOO;

You might consider using a framework to achieve this.您可能会考虑使用框架来实现这一点。 Better still you can use更好的是你可以使用

Include 'functions.php';
require('functions');

Doing OOP is another alternative做 OOP 是另一种选择

IIRC a common solution is a plain file that contains your declarations, that you include in every source file, something like ' constants.inc.php '. IIRC 一个常见的解决方案是包含您的声明的纯文件,您将其包含在每个源文件中,例如“ constants.inc.php ”。 There you can define a bunch of application-wide variables that are then imported in every file.在那里您可以定义一堆应用程序范围的变量,然后将它们导入到每个文件中。

Still, you have to provide the include directive in every single source file you use.尽管如此,您必须在您使用的每个源文件中提供 include 指令。 I even saw some projects using this technique to provide localizations for several languages.我什至看到一些项目使用这种技术为多种语言提供本地化。 I'd prefer the gettext way, but maybe this variant is easier to work with for the average user.我更喜欢 gettext 方式,但对于普通用户来说,也许这个变体更容易使用。

edit For your problem I recomment the use of $GLOBALS[] , see Example #2 for details.编辑对于您的问题,我建议使用$GLOBALS[] ,有关详细信息,请参见示例 #2

If that's still not applicable, I'd try to digg down PHP5 objects and create a static Singleton that provides needed static constants ( http://www.developer.com/lang/php/article.php/3345121 )如果这仍然不适用,我会尝试挖掘 PHP5 对象并创建一个提供所需静态常量的静态单例( http://www.developer.com/lang/php/article.php/3345121

Sessions are going to be your best bet, if the data is user specific, else just use a conifg file.会话将是您最好的选择,如果数据是特定于用户的,否则只需使用配置文件。 config.php:配置文件:

<?php
$EL = "\n<br />\n";
?>

Then on each page add然后在每个页面上添加

require 'config.php'

the you will be able to access $EL on that page.您将能够在该页面上访问 $EL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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