0

I have a function like this:

function form($filename){
    $text="";
    if (file_exists(dirname(__FILE__)."/other/".$filename)){
        ob_start();
        include "other/".$filename;
        $text = ob_get_clean();
    }
    return $text;
}

And, somewhere in my code, I have something like that:

class First {
    public function execute()
        $an_array=array("hi","goodbye");
        return form("my_form.php");
}

Now I would like to know how I could get the values of $an_array on my "my_form.php". The function form is to be used with other files that could need more then one variable.

EDIT

I want that the included file could read more then one parameter. In other words, on some other class, I could have something like this:

class Second {
    public function execute()
        $first_array=array("hi","goodbye");
        $second_array=array("other","another");
        return form("another_form.php");
}

In this case, I would like to read both $first_array and $second_array on my another_form.php file.

EDIT 2

Is there any way to make my form function work like php's array_push function? In other words, I want to have a second parameter on form that acts like the last parameter of array_push.

Cristiano Santos
  • 2,157
  • 2
  • 35
  • 53

3 Answers3

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();
vectorialpx
  • 587
  • 2
  • 17
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();

This way you can pass more than one parameter. $special will be available in my_form.php and will look like this:

Array (
    [first_array] => Array
        (
            [0] => first
            [1] => second
        )

    [second_array] => Array
        (
            [0] => third
            [1] => fourth
        )

)

UPDATE:

You could do it this way, if you don't want to change the variable names

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

In your my_form.php your variables will be available

echo $var1; // test
echo $var2; // test2
Chris
  • 4,255
  • 7
  • 42
  • 83
  • Hmm, I see. But there's no way to the parameter `$special` be dynamic? In other words, something that let me call `form("my_form.php", $array, $array_2, $var_3...)` – Cristiano Santos Feb 07 '13 at 10:19
  • Is is dynamic right now. You can pass as many arrays into the `$array` as you want? – Chris Feb 07 '13 at 10:20
  • Yes, I know. But I would prefer if my variables still the same instead of being inside a 'parent' array. In other words, I want something like what php `array_push` does with the last parameter. – Cristiano Santos Feb 07 '13 at 10:25
  • Hmm, I see. There's no way to do that with `func_get_args()` because I can't get the names of the variables passed as parameters right? – Cristiano Santos Feb 07 '13 at 10:37
  • no you can't, you just get the values of the parameters your passed. That's why I used [extract](http://php.net/manual/en/function.extract.php), which will keep the name & value. – Chris Feb 07 '13 at 10:43
  • I found this [link](http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php#answer-404637). Is it to bad to read the variables this way? – Cristiano Santos Feb 07 '13 at 10:46
  • Why would you even want to use it? What's wrong with my updated code? It worked fine for me. You just have to be careful to not overwrite variables (if you pass a lot of them to `my_form.php`). – Chris Feb 07 '13 at 10:49
  • There's nothing wrong with your code. I just thought that it would be more easy to call something like `form("filename.php",$var_1,$var_2)` then `form("filename.php",array ("var_1"=>$var_1,"var_2"=>$var_2)` in case that someone else needs to use my function. – Cristiano Santos Feb 07 '13 at 10:56
  • 1
    I don't see any difference, except the kind of writing. `form("filename.php",$var_1, $var_2..)` won't be possible if you want to have your function dynamic. The only way to do it is to pass all variables in an array (like I did first) or my second approach. – Chris Feb 07 '13 at 10:59
  • Ok. Sorry for the long chat. I just wanted to be sure that there's no way to do `form("filename.php",$var_1, $var_2..)` with less time consuption. Thank you very much. =) – Cristiano Santos Feb 07 '13 at 11:02
  • No worries, we're here to help ;) – Chris Feb 07 '13 at 11:07
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);
?>
realization
  • 587
  • 1
  • 4
  • 5