Page 1 of 1
vars all starting with m_
Posted: Tue Feb 20, 2007 5:54 am
by thiscatis
Hi!
I got huge class with 40 to 50 vars al starting with m_[something]
Is there something in PHP I can use to make that more simple to handle,
so php finds like a structure in the vars?
Thanks
Posted: Tue Feb 20, 2007 6:04 am
by psychotomus
make a function to add m_ to begining of all it?
Posted: Tue Feb 20, 2007 6:05 am
by Kieran Huggins
use an array?
Code: Select all
$this->m['var1'] = 'something';
$this->m['var2'] = 'something else';
Posted: Tue Feb 20, 2007 8:43 am
by thiscatis
I think I explained it wrong.
I got a form with fields with names like m_something, m_anotherthing
Is there a way to write a function so that when I hit submit all the POST data is converted to variables
like
Code: Select all
$m_something = $_POST['m_something']
etc... without doing that for all fields.
Posted: Tue Feb 20, 2007 8:47 am
by feyd
Blindly converting submissions to variables is asking for trouble. Use an array with known field names. Check if each field name exists in the submission. For those that exist, you can then use
variable variables.
Posted: Tue Feb 20, 2007 11:08 am
by Oren
For your (and others') general knowledge:
extract()
But for this specific question... You'd be better listen to
feyd.
Posted: Tue Feb 20, 2007 1:43 pm
by Mordred
I just can't see what the fuss is all about.
$this->m_variable is perfectly sane, and trying to turn it into this->m['variable'] is in no way shorter, better or more readable.
If you'd rather use $this->variable (i.e. you don't want to type m_ every time), there's find/replace in every decent editor.
As for copying from POST to the members, instead of enumming all POST data and blindly putting it into the object members, do the opposite, enum the members with get_class_vars() and then test to see if each of them is present in $_POST
Edit: Oh, and you should never be in a position to use extract(). It is evil, remove it from your PHP "dictionary".
Posted: Tue Feb 20, 2007 2:10 pm
by RobertGonzalez
I think what he is asking is if there is a way to replicate the functionality of register_globals. At least that is what is sounds like to me.