Jump to content

[SOLVED] Accessing variable inside a method,


play_

Recommended Posts

Can it be done?

 

for example, i have this:

[code=php:0]
<?php

class Formie {

function __construct($settings) {
// Convert the settings array to variables.
	foreach($settings as $key => $value) {
		$this->$key = $value;
	}
}




}

?>

[/code]

 

$settings is an array passed from another page:

[code=php:0]
<?php

// Default settings
$settings = array(
'first_name_length' => 10,
'middle_name_length' => 10,
'last_name_lemgth' => 10,
'email_length' => 60,
'address_length' => 30,
'city_length' => 20,
'zip_length' => 5,
'phone_length' => 10,
'tellfriend_length' => 30,
'comments_length' => 10000,
);

?

[/code]

 

So i include settings.php and then classes.php so that the array values will be set to values.

 

Then i have a form field, like this

<input type="text"  value=" " size="1" name="first_name_length" />

 

on the value property, i would like to use/echo the value of $form->$his->first_name_length. However i know that is incorrect, which is why im asking if there's a way to do so.

 

It does work if i make another method:

function UseVar($var) {
    echo $this->$var;
}

 

and then put this on the form field

<input type="text"  value="<?php $formie->UseVar('first_name_length'); ?>" size="1" name="first_name_length" />

 

But i was wondering if there's a way to just reach a variable inside the class.

what do you mean by this

 

foreach($settings as $key => $value) {
		$this->$key = $value;
	}

 

you will get syntax errror with that

 

No. I have it working.

What it does is, it takes the array $settings. and goes in a loop, the loop takes the keys and assign values, with $this-> in front.

 

so the key 'first_name_length' in the array, becomes $this->first_name_length.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.