[英]Get function variables on included file
我有這樣的功能:
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");
}
在這種情況下,我想在我的another_form.php
文件中讀取$first_array
和$second_array
。
編輯2
有沒有辦法讓我的form
功能像php的array_push
函數一樣工作? 換句話說,我希望在form
上有第二個參數,就像array_push
的最后一個參數一樣。
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();
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
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);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.