0

我想知道是否可以从“包含”中隐藏变量。换句话说,我希望在 PHP 文件中声明的某些类/变量对包含它的任何其他 PHP 文件基本上不可见。这可能吗?有没有办法解决它?

4

3 回答 3

2

方法1:将这些变量/类分离到另一个文件中,只调用它需要的部分。如果您打算在不同的上下文中使用脚本的不同部分,请将它们放在单独的文件中并根据需要调用它们。

方法2:包含文件后,取消设置变量并销毁类。

于 2012-08-05T03:57:15.847 回答
1

恕我直言,我认为是时候介绍PHP5 OOP了。
之后看看可见性

于 2012-08-05T04:03:34.090 回答
0

Your include file could define a function that does all the work, and the variables can be local to that function. As long as you don't declare them global they'll be gone when the function returns.

This function name will be visible to the caller, but it's better than having everything visible.

于 2012-08-05T04:43:00.717 回答