create variables on the fly (dynamically)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bilibytes
    New Member
    • Jun 2008
    • 128

    create variables on the fly (dynamically)

    hi,

    i was wondering how to create variables on the fly,
    by this i mean: i have a pair of strings or an associative array with one string for Key and another for Value:

    Code:
    $var1 = 'hello';
    $var2 = 'johnny';
    or --------------

    Code:
    array('hello'=>'johnny');

    how could i manage to make of these two strings, a vairable with one becoming the name of the variable and the other the value.

    Code:
    echo $hello; //prints johnny

    is there a thing like:

    Code:
    ${$var1} = $var2;
    ?


    Thankyou.
  • bilibytes
    New Member
    • Jun 2008
    • 128

    #2
    The answer is to use eval()

    if you want a variable to have the name of another variable's value, do :

    Code:
    $my_var_name = 'some_text'
    $my_value = 'some other text' 
    
    eval( '$'.$my_var_name.' = \' '. $my_value.' \';' );

    (of course, you cannot do a variable with a wrong syntax: $myvalue ='some text'. 'some text' could never become the name of a variable, even if you write it yourself. $some text = parse error...)

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Yes, you can do that.

      In fact, if you combine your first example with your last example and then echo the variable $hello, you should get "johnny".

      You can also do this using array key=>value pairs by using a foreach loop, like:
      [code=php]
      <?php
      $array = array('hello' => 'John');

      foreach($array as $_key => $_value) {
      ${$_key} = $_value;
      }

      echo $hello;
      ?>
      [/code]
      Which would print "John".

      I have to ask tho, why are you doing this?
      It seems like sort of a backwards logic, taking arrays and importing them into variables.

      Why don't you just use the arrays as they are?
      Why do you want to do $hello rather than $arr['hello']?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by bilibytes
        The answer is to use eval()

        if you want a variable to have the name of another variable's value, do :
        No. eval is never the answer! It's very very dangerous, if not just sloppy, to use eval. It causes all sorts of performance and security issues.

        You can simply do:
        [code=php]
        <?php
        $key = 'hello';
        $value = 'johnny';

        ${$key} = $value;
        ?>
        [/code]

        Comment

        • bilibytes
          New Member
          • Jun 2008
          • 128

          #5
          ok thanks for clarifying.

          could i do:

          Code:
          $this->{key($array)} = $array[key($array)];
          ?

          the point is that i want to make a class that would handle all the HTTP requests of my application.

          and i am trying to put the $_REQUEST array's content into variables of an instance of that class, without having to put to many if conditions.

          is it good to do this?

          i think it clarifies the code but maybe its not a good practice.

          please tell me if what i want to do is totally stupid. maybe its better to just use $_REQUEST['key']...


          as i saw that, in PHP manual, in the extract() function documentation, they talk about using extract to handle $_REQUEST array, i thought it would be great to make an object for that. but there is no option to extract() the array content into object variables" $this->variable". extract() only extracts to normal vairables "$variable" . that is why, i was trying to figure out how to do that...

          long story!

          thankyou

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            So basically, all you are trying to do is;
            [code=php]
            // to change this
            echo $_REQUEST['name'];

            // into this?
            $request = new RequestObject() ;
            echo $request->name;
            [/code]
            Why?

            Unless you plan on having your class automatically sanitize or validate your fields?

            I usually get request variables via a controller class that automatically sanitizes the data, but it doesn't need to be as complicated as you are trying to make it.
            A simple static "getField($key) " method that gets and processes the field before returning it to you usually does fine.

            Comment

            Working...