Jump to content

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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.