PHP Classes

Why would you want to make constants out of post data?

Recommend this page to a friend!

      Capturing data in a simplified form post  >  All threads  >  Why would you want to make constants...  >  (Un) Subscribe thread alerts  
Subject:Why would you want to make constants...
Summary:Is it safe to create constants out of post data?
Messages:5
Author:Rineez Ahmed
Date:2011-10-28 04:25:43
Update:2011-12-08 03:05:13
 

 


  1. Why would you want to make constants...   Reply   Report abuse  
Picture of Rineez Ahmed Rineez Ahmed - 2011-10-28 04:25:44
I'm wondering whether making constants out of post data will be safe and good practice to do.

  2. Re: Why would you want to make constants...   Reply   Report abuse  
Picture of Alisson Acioli Alisson Acioli - 2011-10-28 18:59:57 - In reply to message 1 from Rineez Ahmed
It's safe. is the same method as $ _POST, it just replaces the names of variables by constants. This class has the same function as $ _POST. Only simplifies how to use them.

  3. Re: Why would you want to make constants...   Reply   Report abuse  
Picture of Kyle Barrett Kyle Barrett - 2011-11-02 18:44:26 - In reply to message 1 from Rineez Ahmed
It could cause conflict if you post a form field with a name of an existing constant. Another possible hiccup would be that when a constant value CONSTANT_NAME isn't defined it will not default to NULL, but PHP will assume you meant a string of "CONSTANT_NAME"; though this shouldn't be an issue with proper validation.

In a multi-tier application, you'd really be mucking about with mixing user-input and business logic (you have this problem with a $_POST superglobal as well, but atleast it's easily identifiable as a superglobal). Constants should be reserved for constant variables. User input by definition isn't constant; if the user doesn't make a submission, then the constant won't exist. Not really "wrong" in the sense that it won't work, but wrong in the sense that you're using a screw driver to hammer a nail.

  4. Re: Why would you want to make constants...   Reply   Report abuse  
Picture of Rineez Ahmed Rineez Ahmed - 2011-11-03 15:42:02 - In reply to message 3 from Kyle Barrett
Thanks for your reply @Kyle Barrett. That was exactly the concern I had.
Using constants in a place where a variables should be used will probably complicate things rather than simplifying.
One can argue that a user input remains constant as far as the duration of a single request-response cycle is concerned. But even that will not convince me it is a right way to deal with user data. The $_POST array keeps the post data completely isolated from other data in your program.
I feel that creating constants out of post could be compared to emptying a waste bin all over the floor.

  5. Re: Why would you want to make constants...   Reply   Report abuse  
Picture of Alisson Acioli Alisson Acioli - 2011-12-08 03:05:13 - In reply to message 4 from Rineez Ahmed
I fully agree with your answer @Kyle, this class can be used for something more simple as display data or simply make a check if there are any empty field without getting making "empty" all the time. In one of my scripts, I found some problems when using constants for inserting in the database, so I use this class to do only checks if the field is empty.
Well, now each one will adapt this script to your needs.