0

我有这样的功能:

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");
}

现在我想知道如何获得$an_array“my_form.php”的值。该函数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.

4

3 回答 3

1

讨厌的一个

<?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();
于 2013-02-07T09:55:03.307 回答
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
于 2013-02-07T10:13:29.857 回答
0

文件: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;
}

?>

文件:其他/my_form.php

<?php
print_r($params);
?>
于 2013-02-07T09:59:05.677 回答