Page 1 of 1

extract() function

Posted: Mon Feb 17, 2003 4:09 pm
by sweahe
Is it somehow possible to use the extract() function to make the extracted vars to be object member vars inside a class... so I can access them like $this->myvar;

$my_array = array('myvar' => 23, 'next_var' => 16);

extract($my_array);

Any ideas?

Posted: Mon Feb 17, 2003 6:53 pm
by Stoker
I don't think extract() has any OO/instance awareness, but it should be possible in some way, something like this, untested

Code: Select all

<?php

 class test {
    
      function extract_to_object ($array) {
         if (is_array($array)) foreach ($array as $name => $value) {
              $this->$name = $value; // There is no error control here now..
         }
      }   
      
  }

  $test = new test ();
  $myarray = array( 'beer' => 'Sam Adams' , 'wine' => 'Torres Gran Coronas 1987' );
  $test->extract_to_object($myarray);
  echo 'I like '.$test->beer.' and '.$test->wine;

?>

Posted: Mon Feb 17, 2003 10:36 pm
by McGruff
In newer versions of php you have to name all the vars first, before defining any functions. Something like the code in the other post would do as a constructor, but wouldn't work on its own because the var's haven't been named yet.

If the class is in a file of its own, you could try writing over the file before use: replace old var list lines with current $varlist:

foreach($array as $key => $value) {
$varlist .= "var \$" . $key . ";\n";
}

Then feed the original array to a constructor function to initialise the variables.

Or store the class code in a database field and eval() with latest $varlist each time?

Neither sounds very practical I must admit.

Posted: Tue Feb 18, 2003 12:30 am
by sweahe
I have done something like Stoker's example and I know it works, I just hoped I could do it more efficiently, nicer and quicker with some built in php function... but maybe I'll have to wait until php version 5?

Thanks anyway!
/Andreas

Posted: Tue Feb 18, 2003 7:51 am
by Stoker
McGruff wrote:In newer versions of php you have to name all the vars first, before defining any functions. Something like the code in the other post would do as a constructor, but wouldn't work on its own because the var's haven't been named yet.
Hmm, 4.3.0 oiffers better object protection? The function I posted works with my 4.3.0 setup.. I never tried, but I would hope that you are not allowed to create a new var inside an object from the outside..

Posted: Tue Feb 18, 2003 9:34 am
by McGruff
This is what I meant (manual):

Code: Select all

<?php
<?php
/* None of these will work in PHP 4. */
class Cart
{
    var $todays_date = date("Y-m-d");
    var $name = $firstname;
    var $owner = 'Fred ' . 'Jones';
    var $items = array("VCR", "TV");
}

/* This is how it should be done. */
class Cart
{
    var $todays_date;
    var $name;
    var $owner;
    var $items;

    function Cart()
    {
        $this->todays_date = date("Y-m-d");
        $this->name = $GLOBALS['firstname'];
        /* etc. . . */
    }
}
?>
?>
I couldn't see how the constructor would work without naming the class vars first - if you've got it working I must be wrong.

Posted: Tue Feb 18, 2003 9:55 am
by Stoker
ah, perhaps you missunderstood slightly; It was to assign the content of an array to the current instance symbol table, just as extyract() does to global.. si it was not about getting all globals assigned to something in an instance..

Anyway, although it is not oo-ethical, in PHP there is no need to define with var, you can (from the inside) generate any var you want in the object, so I am not using any constructor, just calling a method (function), passing it an array and that function assigns new vars in the object accordingly..