如何从 function() 内的 include() 加载变量?

发布于 2024-11-16 18:38:58 字数 553 浏览 0 评论 0原文

我正在尝试从 .php 文件中获取一些变量。我调用一个函数,其中包含一个包含文件,其中包含我想要的变量,但它不会返回任何内容。在函数内我可以查看变量值,但不能在函数外部查看。

//MODULE INCLUDE
function moduleInclude($mid) {
$query = "SELECT file,type FROM sh_module WHERE mid = '{$mid}'";
$exe = mysql_query($query);
$row = mysql_fetch_array($exe);
$folder = moduleFolder($row['type']);
$output = include("../includes/modules/".$folder."/".$row['file']);

return $output;
}

//Load the vars
moduleInclude($mod_ship);
$ship_meth = $MODULE_title_client;
//THE ^^^^ MODULE_title_client never returns anything :(

I'm trying to get some variables from a .php file. I call a function which has an included file in it that has the vars I want but it won't return any. Within the function I can view the variables values, but not outside the function.

//MODULE INCLUDE
function moduleInclude($mid) {
$query = "SELECT file,type FROM sh_module WHERE mid = '{$mid}'";
$exe = mysql_query($query);
$row = mysql_fetch_array($exe);
$folder = moduleFolder($row['type']);
$output = include("../includes/modules/".$folder."/".$row['file']);

return $output;
}

//Load the vars
moduleInclude($mod_ship);
$ship_meth = $MODULE_title_client;
//THE ^^^^ MODULE_title_client never returns anything :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清音悠歌 2024-11-23 18:38:59

include 不会返回文件 include 的内容,只是将该文件的代码放在包含该文件的位置。

文件中声明的变量在 include 语句之后可用。但是当您尝试在函数外部访问它们时,变量将无法访问,因为它们现在是函数范围。

您可以更新代码以包含函数外部的该文件。

function moduleInclude($mid) {
$query = "SELECT file,type FROM sh_module WHERE mid = '{$mid}'";
$exe = mysql_query($query);
$row = mysql_fetch_array($exe);
$folder = moduleFolder($row['type']);
return "../includes/modules/".$folder."/".$row['file'];
}

//Load the vars
$include_path=moduleInclude($mod_ship);
include $include_path;
$ship_meth = $MODULE_title_client;

include does not return the content of file include just put the code of that file at the place where the file is included.

And the variables declared in the files are available after the include statement. but as you are trying to access them outside the function the variable will not be accessible as they are now function scope.

You could update your code to include that file from outside the function.

function moduleInclude($mid) {
$query = "SELECT file,type FROM sh_module WHERE mid = '{$mid}'";
$exe = mysql_query($query);
$row = mysql_fetch_array($exe);
$folder = moduleFolder($row['type']);
return "../includes/modules/".$folder."/".$row['file'];
}

//Load the vars
$include_path=moduleInclude($mod_ship);
include $include_path;
$ship_meth = $MODULE_title_client;
久而酒知 2024-11-23 18:38:59

我认为你可以使用这样的东西:

//MODULE INCLUDE
function moduleInclude($mid) {
    $query = "SELECT file,type FROM sh_module WHERE mid = '{$mid}'";
    $exe = mysql_query($query);
    $row = mysql_fetch_array($exe);
    $folder = moduleFolder($row['type']);
    include("../includes/modules/".$folder."/".$row['file']);
    return get_defined_vars();
}

//Load the vars from include file into array
$module_vars=moduleInclude($mod_ship);
//Use the desired variable
$ship_meth = $module_vars['MODULE_title_client'];

如果我理解正确的话。它真的很难看,我建议你了解更多关于变量范围使代码变得更好,因为包含在函数中几乎总是错误的方法。我认为我们需要知道包含文件“../includes/modules/”.$folder.”/”.$row['file'] 中发生了什么,以更好地帮助您。

I think you can use something like this:

//MODULE INCLUDE
function moduleInclude($mid) {
    $query = "SELECT file,type FROM sh_module WHERE mid = '{$mid}'";
    $exe = mysql_query($query);
    $row = mysql_fetch_array($exe);
    $folder = moduleFolder($row['type']);
    include("../includes/modules/".$folder."/".$row['file']);
    return get_defined_vars();
}

//Load the vars from include file into array
$module_vars=moduleInclude($mod_ship);
//Use the desired variable
$ship_meth = $module_vars['MODULE_title_client'];

if I understood you correctly. It is really ugly and I suggest you learn more about variable scopes to make this code better as including in a function is almost always the wrong way to go. I think we need to know what hapens inside your include file "../includes/modules/".$folder."/".$row['file'] to help you better.

ζ澈沫 2024-11-23 18:38:59

只需在包含文件末尾返回一个对象:

$foo = 1;
$bar = 2;
return array('foo' => $foo, 'bar' => $bar)

然后, include 将返回模块中返回的数组。

Just return an object at the end of your include file:

$foo = 1;
$bar = 2;
return array('foo' => $foo, 'bar' => $bar)

Then, include will return the array returned in the module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文