我有这样的功能:
function form($filename){
$text="";
if (file_exists(dirname(__FILE__)."/other/".$filename)){
ob_start();
include "other/".$filename;
$text = ob_get_clean();
}
return $text;
}
而且,在我的代码中,我有类似的东西:
class First {
public function execute()
$an_array=array("hi","goodbye");
return form("my_form.php");
}
现在我想知道如何在我的“my_form.php”中获取$an_array
的值。
函数form
将与其他可能需要多个变量的文件一起使用。
修改
我希望包含的文件可以读取多个参数。换句话说,在其他一些课上,我可以有这样的东西:
class Second {
public function execute()
$first_array=array("hi","goodbye");
$second_array=array("other","another");
return form("another_form.php");
}
在这种情况下,我想在我的$first_array
文件中阅读$second_array
和another_form.php
。
编辑2
有没有办法让我的form
函数像php的array_push
函数一样工作?换句话说,我想在form
上设置第二个参数,其作用类似array_push
的最后一个参数。
答案 0 :(得分:1)
NASTY ONE
<?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();
答案 1 :(得分:1)
function form($filename, $special = array()){
$text="";
$FILE = $filename;
if (file_exists(dirname(__FILE__)."/other/".$filename)){
ob_start();
include $FILE;
$text = ob_get_clean();
}
return $text;
}
class Copy {
public function execute() {
$array = array();
$array['first_array'] = array( "first", "second" );
$array['second_array'] = array( "third", "fourth" );
return form("my_form.php", $array);
}
}
$copy = new Copy();
echo $copy->execute();
这样您就可以传递多个参数。 $special
将在my_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
答案 2 :(得分:0)
file: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();
?>
file: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;
}
?>
file:other / my_form.php
<?php
print_r($params);
?>