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
vars all starting with m_
Moderator: General Moderators
-
psychotomus
- Forum Contributor
- Posts: 487
- Joined: Fri Jul 11, 2003 1:59 am
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
use an array?
Code: Select all
$this->m['var1'] = 'something';
$this->m['var2'] = 'something else';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 etc... without doing that for all fields.
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']- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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".
$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".
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA