PHP:如何访问已在其外部声明的函数内部的变量? [英] PHP: how to access variables inside a function that have been declared outside of it?

查看:32
本文介绍了PHP:如何访问已在其外部声明的函数内部的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每个页面的最开始,我都包含一个启动 SESSION 等的文件,我们称之为 session.php.在这个文件 session.php 中,我包含了一个包含函数的文件,我们称之为 function1.php,因为我需要该函数在 session 中可用.php.

At the very beginning of each page I include a file that starts the SESSION etc, lets call it session.php. In this file session.php, I include a file that contains a function, let's call it function1.php, because I need the function to be available in session.php.

但是,稍后在主页中我还包含了 function2.php,它需要访问 session.php 中设置的变量,所以我另外尝试包含 function2.php 中的 session.php.

However, later in the main page I also include function2.php which needs to access variables set in session.php, so I additionally tried to include session.php in function2.php.

问题是出现了一个错误,因为 function1 将被多次声明...

The problem is that an error occurs as function1 will be declared multiple times...

致命错误:无法重新声明 function1()(之前声明于 ...

Fatal error: Cannot redeclare function1() (previously declared in ...

那么,对此有什么更优雅、更干净(呃)的解决方案?你怎么解决的?

So, what would be a more elegant and clean(er) solution for this? How could you solve it?

基本上,我需要访问之前已包含在主页中的函数内的变量...

Basically, I'd need to access variables inside a function that have been included in the main page before...

推荐答案

这不是公共和私有变量的问题,因为您想要从 session.php 获得的变量不是对象的一部分.它们只是您在全局范围内(可能)声明的变量,现在需要在 function2.php 中访问.

This isn't an issue with public and private variables, since the variables you want from session.php aren't part of an object. They're just variables which you declared (probably) in the global scope, and now need to access in function2.php.

第一:function2.php 的内容是不是一个函数?如果不是,那么该文件应该与包含它的脚本中的位置具有相同的范围,并且它应该能够很好地访问它们.

First: are the contents of function2.php... a function? If not, then that file should have the same scope as the location in your script from which it was included, and it should be able to access them just fine.

如果您需要访问 session.php 变量的地方是函数或对象,您有几个选择.

If the place from which you need to access the session.php variables is a function, or an object, you have a few choices.

0.重新包含文件

这就是您现在要做的.它失败是因为 session.php 还包含一个函数定义,一旦它被创建,你就不能重新定义一个函数.作为一种解决方法,您可以将变量定义移动到一个单独的文件中,并将其包含在 function2 中.这很笨拙,但很简单.

This is what you're trying to do now. It fails because session.php also contains a function definition and you can't re-define a function once it's been created. As a workaround, you could move the variable definitions into a separate file and just include that in function2. This is clumsy, but simple.

1.调用时将变量传递给函数.

  • 优点:简单灵活.
  • 缺点:调用函数有点痛苦.您可以将它们全部放入一个参数数组中,而不是分别传递每个参数,从而简化这一过程.

2.使用全局

在 function2 的开头,只需添加命令 global $var1, $var2 ... 以导入脚本中其他地方使用的任何变量.

At the beginning of function2, just add the command global $var1, $var2 ... to import any variables that were used elsewhere in the script.

这也很简单,但是许多程序员认为使用全局变量是草率的,并且会让您在其他地方遇到难以调试的错误.

This is also very simple, but using global variables is seen by many programmers as sloppy and opens you up to hard-to-debug errors elsewhere.

3.使用常量

在 session.php 中,定义要共享为常量的变量.例如,define("PASSWORD", "coolpassword123").常量可以从函数或对象等内部引用,并且在声明后永远不能更改.例如,echo(PASSWORD)(没有引号,没有 $).

In session.php, define the variables you want to share as constants. e.g., define("PASSWORD", "coolpassword123"). Constants can be referenced from inside functions or objects, etc., and can never be changed after being declared. e.g., echo(PASSWORD) (no quotes, no $).

常量通常是此类问题的最佳解决方案,只要 1) 您不需要更改它们的值,并且 2) 您可以坚持使用简单的数字或字符串值.常量不能是数组、对象等.

Constants are generally the best solution to this kind of problem as long as 1) you don't need to change their value, and 2) you can stick to simple numeric or string values. A constant can't be an array, object, etc.

这篇关于PHP:如何访问已在其外部声明的函数内部的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆