duanpo2037
2018-01-19 17:41
浏览 161
已采纳

如何使用函数将全局变量包含在另一个函数中?

I have a rather large set of global variables that I want to be able to use within other functions across various files. I know that to use these variables within other functions I need to use the global keyword to include the variable names.

Everything works when I do that, but since there are so many variables, I'd rather create a function that allows me to include the global declaration within the function, with the specific chunk of variables I need being included using a function parameter.

For illustrative purposes, this works:

variables.php (declared before other files are included)

$var1 = 'abc';
$var2 = 'def';
$var3 = 'ghi';

other-function-file.php

function my_function() {
  global $var1, $var2, $var3;
  return $var1 . $var2 . $var3;
}

But this does not work:

variables.php

$var1 = 'abc';
$var2 = 'def';
$var3 = 'ghi';

globals-list-declaration.php

global $var1, $var2, $var3;

globals-include-function.php

function include_globals() {
  include_once('globals-list-declaration.php');
}

other-function-file.php

function my_function() {
  include_globals();
  return $var1 . $var2 . $var3;
}

This is a little simplified, as there multiple variable and global declaration files, and I'll be able to specify which file to include by passing in a parameter to include_globals() in globals-include-function.php.

But for some reason, when I move the hard-typed global $var1, $var2, $var3; to a seperate file and include it in the exact same location (either through include or the include_globals() function decribed above), it ceases to work.

How can I make this work or is it even possible?

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我有一组相当大的全局变量,我希望能够在各种文件的其他函数中使用它们。 我知道要在其他函数中使用这些变量,我需要使用 global 关键字来包含变量名。

当我这样做时,一切都有效,但是 有这么多变量,我宁愿创建一个函数,允许我在函数中包含 global 声明,我需要使用函数参数包含特定的变量块。

为了便于说明,这有效:

variables.php (在包含其他文件之前声明)

  $ var1 ='abc'; 
 $ var2 ='def'; 
 $ var3 ='ghi'; 
    
 
 

other-function-file.php

  function my_function(){
 global $ var1,$ var2,$  var3; 
返回$ var1。  $ var2。  $ var3; 
} 
   
 
 

但这不起作用:

variables.php

  $ var1 ='abc'; 
 $ var2 ='def'; 
 $ var3 ='ghi'; 
   \  n 
 

globals-list-declaration.php

  global $ var1,$ var2,$ var3; 
    
 
 

globals-include-function.php

  function include_globals(){
 include_once('globals  -list-declaration.php'); \ N} 
   
 
 

其他功能-file.php \ n

  function my_function(){
 include_globals(); 
返回$ var1。  $ var2。  $ var3; 
} 
   
 
 

这有点简化,因为有多个变量和全局声明文件,我将能够指定要包含的文件 通过将参数传递给 globals-include-function.php 中的 include_globals()

但出于某种原因,当我 将硬类型的全局$ var1,$ var2,$ var3; 移动到一个单独的文件中,并将 include 移动到完全相同的位置(通过 include 或上面描述的 include_globals()函数,它停止工作。

我怎样才能完成这项工作或者甚至可以做到这一点?

谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongshuan8722 2018-01-19 17:51
    最佳回答

    From the docs http://php.net/manual/en/function.include.php:

    If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function

    In other words, if can't use this:

    function include_globals() {
        include_once('globals-list-declaration.php');
    }
    

    Because the definition of globals will only exists inside this function.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题