extract() function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

extract() function

Post 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?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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;

?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post 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
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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..
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Thu Aug 11, 2005 2:54 pm, edited 1 time in total.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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..
Post Reply