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)
?>