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 4553308
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:55:38+00:00 2026-05-21T16:55:38+00:00

I am new to the world of coding as well as PHP and just

  • 0

I am new to the world of coding as well as PHP and just ventured into the territory of objects, classes and methods. I am having a hard time understanding how to use object oriented coding in the following code I am attempting to put together. I have a form that users are required to fill in their first name, last name and email address. As a first step I am validating if the user has filled in any of the data

Hence my code looks as such

class myform {

var $firstname, $lastname, $email;

function User($firstname, $lastname, $email) {
$fname = isset($_POST['fname']) ? $_POST['fname'] : '';
$lname = isset($_POST['lname']) ? $_POST['lname'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';

errormsg = array():

if(empty($fname)) {
            $errormsg[0] = 'Please enter your first name';
        }

        if(empty($lname)) {
            $errormsg[1] = 'Please enter your last name';
        }

        if(empty($email)) {
            $errormsg[2] = 'Please enter your email address';
        }
}
}
}

Now within my HTML page I am instantiating the object

<?php
$validate = myform();
$validate->User($firstname, $lastname, $email)
?>

When I submit my form, I get an error that the variables $firstname, $lastname, $email have not been defined unless I change the function to read as below which I understand to be referencing the variables as opposed to copying.

function User(&$firstname, &$lastname, &$email)

Now the second question I have is that I have read that I shouldn’t assign values to the data members or variables as follows

<?php
    $validate = myform();
    $validate->firstname = isset($_POST['fname']) ? $_POST['fname'] : '';
    $validate->User($firstname, $lastname, $email)
    ?>
  • 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-21T16:55:38+00:00Added an answer on May 21, 2026 at 4:55 pm

    Since these are class variables you shouldn’t need to set them the way you are… User operates like a regular function so in your code that you posted as your HTML code you’re saying “In the instance of the form named ‘validate’ run the function User with the values null, null and null’. Your class already declares those 3 values, so you can cut them out of your declaration.

    That being said classes still utilize private and protected variables, so inside of your User function if you put $username = 'foo'; it’s not going to change the value of the variables for the class itself. For that you need $this->firstname = $_POST['fname'];

    You’re fundamentally flawed in the way you’re thinking about your class, and you should really build a function that sets the firstname, lastname and email and then call that function from inside the class…

    class myform {
    
    public $firstname, $lastname, $email;
    
    function setUsername($value) {
        //Add Form Input Sterilization here then
        $this->firstname = $value;
        }
    
    }
    

    Then in your main code you would just call

    $validate = new myform;
    $validate->setUsername($_POST['fname']);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to the world of coding as well as CSS. Having read
I am new to the world of coding as well as PHP and befuddled
I am new to the world of coding as well as PHP and am
I am new to the world of coding as well as php and am
I am new to the world of coding, XHTML, CSS as well as PHP.
I am new to the world of coding, PHP and object oriented programming. I
I am new to the world of coding as well as XHTML. I am
I am new to the world of coding as well as CSS. I have
I am new to the world of coding, XHTML, CSS and PHP. This is
I am new to coding and would just like to know a bit more

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.