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?
extract() function
Moderator: General Moderators
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;
?>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.
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.
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 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.
This is what I meant (manual):
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.
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. . . */
}
}
?>
?>
Last edited by McGruff on Thu Aug 11, 2005 2:54 pm, edited 1 time in total.
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..
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..