Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6933997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:54:14+00:00 2026-05-27T11:54:14+00:00

I am currently creating a class to handle all my form data which will

  • 0

I am currently creating a class to handle all my form data which will then be placed into a database for future use.

Currently in my class constructor I am using the isset() function on an individual basis when I construct my variables from POST.

function __construct()
    {
            if (isset($_POST['first_name']))
            {
                 $this->first_name = $_POST['first_name'];
            }
            else
            {
                 $this->first_name = "NULL";
            }

    }

Although this example is just for one variable, I am doing this for another 12 in my php application.

As the old adage says “You cannot repeat code”.

So I am trying to create a member function in my class that will be able to handle all variables, like a get/set function.

So that in my constructor all I need to do is this:

function __construct()
    {
            //first parameter is the member variable to be set, second parameter is the element name that the value will be POST from
           $firstname = $this->setter($firstname, "first_name");

    }

//what do I put here?
    function setter(first parameter, second parameter)
        {
                //checks the post variable has been set
                if ( isset($_POST[second_parameter]))
                return $_POST['second_parameter'];  
                else
                return "NULL";

        }

Can someone point me in the right direction, or with a code sample.

The aspect I am not sure about is how do I set the function to handle two parameters with a generic handle?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:54:14+00:00Added an answer on May 27, 2026 at 11:54 am

    first of all, this is a sytax error (I guess you know)

    function setter(first parameter, second parameter)
    

    also, this should issue an warning and lead to other behavior than what you want

    isset($_POST[second_parameter]
    

    you are using an undefined constant that after the warning evaluates to the string “second_parameter”, you should use the variable

    also, here you are using a string and not a variable:

    return $_POST['second_parameter']; 
    

    another problem is the fact that you return the values, and you don’t set them as members in your class

     if ( isset($_POST[second_parameter]))
                    return $_POST['second_parameter'];  
                    else
                    return "NULL";
    

    so maybe something like

    function setter($first_parameter, $second_parameter)
    {
            //checks the post variable has been set
            if ( isset($_POST[$second_parameter]))
            {
                $this->$first_parameter = $_POST[$second_parameter];  
            }
            else
            {
                $this->$first_parameter = NULL;
            }
    }
    

    might be closer to what you need

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently creating an EJB3 Data Access Class to handle all database operations in
I'm currently creating an explicit reference to this in the outer class so that
Currently I have created a ABCFactory class that has a single method creating ABC
I'm creating my first game, and I've currently set up a 'GameState' class, to
I'm currently creating an application for a customer that will allow them to automatically
I'm currently creating a WinForm in VB.NET bound to an access database. Basically what
We are currently creating an InfoPath 2007 form is deployed in SharePoint 2007. In
I am currently creating a Perl module for in house use. I used ExtUtils::ModuleMaker
in my ASP.NET application, I am creating a new class to handle errors in
I have started creating an app which uses a core data stack at the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.