vars all starting with m_

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

vars all starting with m_

Post 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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

make a function to add m_ to begining of all it?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

use an array?

Code: Select all

$this->m['var1'] = 'something';
$this->m['var2'] = 'something else';
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

For your (and others') general knowledge: extract()

But for this specific question... You'd be better listen to feyd.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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".
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply