Php 获取包含文件上的函数变量
我有这样一个函数:Php 获取包含文件上的函数变量,php,function,variables,parameters,include,Php,Function,Variables,Parameters,Include,我有这样一个函数: function form($filename){ $text=""; if (file_exists(dirname(__FILE__)."/other/".$filename)){ ob_start(); include "other/".$filename; $text = ob_get_clean(); } return $text; } class Second { publi
function form($filename){
$text="";
if (file_exists(dirname(__FILE__)."/other/".$filename)){
ob_start();
include "other/".$filename;
$text = ob_get_clean();
}
return $text;
}
class Second {
public function execute()
$first_array=array("hi","goodbye");
$second_array=array("other","another");
return form("another_form.php");
}
Array (
[first_array] => Array
(
[0] => first
[1] => second
)
[second_array] => Array
(
[0] => third
[1] => fourth
)
)
在我的代码中,我有这样的东西:
class First {
public function execute()
$an_array=array("hi","goodbye");
return form("my_form.php");
}
现在我想知道如何在我的“my_form.php”上获得$an_array
的值。
函数form
用于可能需要一个以上变量的其他文件
编辑
我希望包含的文件可以读取多个参数。换句话说,在另一节课上,我可以做这样的事情:
function form($filename){
$text="";
if (file_exists(dirname(__FILE__)."/other/".$filename)){
ob_start();
include "other/".$filename;
$text = ob_get_clean();
}
return $text;
}
class Second {
public function execute()
$first_array=array("hi","goodbye");
$second_array=array("other","another");
return form("another_form.php");
}
Array (
[first_array] => Array
(
[0] => first
[1] => second
)
[second_array] => Array
(
[0] => third
[1] => fourth
)
)
在本例中,我想在我的另一个表单.php
文件上阅读$first\u array
和$second\u array
编辑2
有没有办法让我的
表单
函数像php的数组推送
函数一样工作?换句话说,我想在表单上有第二个参数,它的作用类似于数组的最后一个参数
<?php
function form($filename){
$text = '';
$FILE = dirname(__FILE__)."/other/".$filename;
$SPECIALVARS = func_get_args();
$allVars = explode(',', $SPECIALVARS[1]);
foreach( $allVars as &$AV ) { $AV = trim($AV); }
foreach( $SPECIALVARS as $k => $v ) {
if( $k > 1 ) {
$theKey = $k-2;
$_tmp = str_replace('$', '', $allVars[$theKey]);
// var_dump($_tmp);
$$_tmp = $v;
}
}
// var_dump($first_array); // now we have it
if (file_exists($FILE)){
ob_start();
include $FILE; // here you can use your vars
$text = ob_get_clean();
}
return $text;
}
class Copy {
public function execute() {
$an_array = array( "hi", "goodbye" );
return form("my_form.php", '$an_array', $an_array);
}
}
class Second {
public function execute() {
$first_array = array( "hi", "goodbye" );
$second_array = array( "other", "another" );
return form("another_form.php", '$first_array, $second_array', $first_array, $second_array);
}
}
$s = new Second;
$s->execute();
文件:test.php
include "funcs.php";
class Copy {
public function execute()
{
$an_array=array("hi","goodbye");
return form("my_form.php",$an_array);
}
}
$f=new Copy();
echo $f->execute();
?>
文件:funcs.php
<?php
function form($filename, $params){
$text="";
if (file_exists(dirname(__FILE__)."/other/".$filename)){
ob_start();
include "other/".$filename;
$text = ob_get_clean();
}
return $text;
}
?>
文件:other/my_form.php
<?php
print_r($params);
?>
这样可以传递多个参数<代码>$special
将在my_form.php中提供,如下所示:
function form($filename){
$text="";
if (file_exists(dirname(__FILE__)."/other/".$filename)){
ob_start();
include "other/".$filename;
$text = ob_get_clean();
}
return $text;
}
class Second {
public function execute()
$first_array=array("hi","goodbye");
$second_array=array("other","another");
return form("another_form.php");
}
Array (
[first_array] => Array
(
[0] => first
[1] => second
)
[second_array] => Array
(
[0] => third
[1] => fourth
)
)
更新:
如果不想更改变量名,可以这样做
function form($filename, $special = array()){
$text = '';
if (file_exists(dirname(__FILE__)."/other/".$filename)){
extract($special);
ob_start();
include $FILE;
$text = ob_get_clean();
}
return $text;
}
class Copy {
public function execute() {
return form("my_form.php", array(
'var1' => 'test',
'var2' => 'test2'
));
}
}
$copy = new Copy();
echo $copy->execute();
在my_form.php
中,变量将可用
echo $var1; // test
echo $var2; // test2
但是如果我有一个以上的参数要发送到表单
,那该怎么办呢?请参阅我的编辑,以获取我在第一条评论中试图说的内容的示例。嗯,我明白了。但是没有办法让参数$special
成为动态的?换句话说,让我调用表单(“my_form.php”)、$array、$array、$array_2、$var_3……
的东西现在是动态的。您可以根据需要将任意多个数组传递到$array
中?是的,我知道。但我更希望我的变量保持不变,而不是在“父”数组中。换句话说,我想要像phparray\u push
对最后一个参数所做的那样。嗯,我明白了。使用func\u get\u args()
无法做到这一点,因为我无法正确获取作为参数传递的变量的名称?我看不到任何区别,除了写入的类型<如果您想使函数具有动态性,则代码>表单(“filename.php”、$var_1、$var_2..)
是不可能的。唯一的方法是在数组中传递所有变量(就像我第一次做的那样)或我的第二种方法。我应该如何在另一个
中获取变量内容?我是否需要调用$SPECIALVARS[1]
来获取$first\u array
值?您也可以进行黑客攻击!)我怎么能用传递给他们的名字来称呼他们呢?这就是问题所在。现在你可以这样做,但你也必须发送姓名<代码>返回表单(“other_form.php”、“$first_array、$second_array”、$first_array、$second_array”)代码>。检查更新的代码。