【问题标题】:Get function variables on included file获取包含文件的函数变量
【发布时间】:2013-01-22 18:23:49
【问题描述】:

我有这样的功能:

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 的最后一个参数。

【问题讨论】:

    标签: php function variables parameters include


    【解决方案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
    

    【讨论】:

    • 嗯,我明白了。但是参数$special没有办法是动态的吗?换句话说,让我打电话给form("my_form.php", $array, $array_2, $var_3...)
    • 现在是动态的。您可以将任意数量的数组传递给$array
    • 是的,我知道。但如果我的变量仍然相同而不是在“父”数组中,我更愿意。换句话说,我想要像 php array_push 对最后一个参数所做的那样。
    • 嗯,我明白了。 func_get_args() 无法做到这一点,因为我无法获取作为参数传递的变量的名称吗?
    • 我看不出有什么不同,除了写作的种类。如果你想让你的函数动态化,form("filename.php",$var_1, $var_2..) 是不可能的。唯一的方法是传递数组中的所有变量(就像我第一次做的那样)或我的第二种方法。
    【解决方案2】:

    讨厌的人

    <?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();
    

    【讨论】:

    • 如何获取another_form.php中的变量内容?我需要调用$SPECIALVARS[1] 来获取$first_array 的值吗?
    • 你也可以做一个hack! ;)
    • 我怎样才能进行黑客攻击,以便用他们传递的同名来称呼他们?
    • 这就是重点。现在您可以这样做,但您还必须发送姓名。 return form("another_form.php", '$first_array, $second_array', $first_array, $second_array);。检查更新的代码。
    【解决方案3】:

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

    【讨论】:

    • 但是如果我有多个参数要发送到form 怎么办?
    • 查看我的编辑,了解我在第一条评论中想要表达的内容
    猜你喜欢
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2012-05-21
    • 2016-08-06
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多