play_ Posted August 8, 2007 Share Posted August 8, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63831-solved-accessing-variable-inside-a-method/ Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 what do you mean by this foreach($settings as $key => $value) { $this->$key = $value; } you will get syntax errror with that Quote Link to comment https://forums.phpfreaks.com/topic/63831-solved-accessing-variable-inside-a-method/#findComment-318132 Share on other sites More sharing options...
trq Posted August 8, 2007 Share Posted August 8, 2007 You might want to take a look at member overloading. Quote Link to comment https://forums.phpfreaks.com/topic/63831-solved-accessing-variable-inside-a-method/#findComment-318133 Share on other sites More sharing options...
play_ Posted August 8, 2007 Author Share Posted August 8, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63831-solved-accessing-variable-inside-a-method/#findComment-318136 Share on other sites More sharing options...
play_ Posted August 8, 2007 Author Share Posted August 8, 2007 Thanks thorpe, ill have a look Quote Link to comment https://forums.phpfreaks.com/topic/63831-solved-accessing-variable-inside-a-method/#findComment-318137 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.