PeanutsMonkey
PeanutsMonkey

Reputation: 7095

PHP Class and Methods

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

Upvotes: 1

Views: 301

Answers (3)

user719852
user719852

Reputation: 131

you have to pass the values in the function call i suspect whether values have been set for

$firstname,$lastname,$email
$validate->User($firstname, $lastname, $email)

Upvotes: 0

DaOgre
DaOgre

Reputation: 2100

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']);

Upvotes: 2

morgar
morgar

Reputation: 2407

BTW, you must know var is deprecated in PHP5, you should use public, protected or private.

"Note: In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private. However, var is no longer required. In versions of PHP from 5.0 to 5.1.3, the use of var was considered deprecated and would issue an E_STRICT warning, but since PHP 5.1.3 it is no longer deprecated and does not issue the warning. If you declare a property using var instead of one of public, protected, or private, then PHP 5 will treat the property as if it had been declared as public."

http://www.php.net/manual/en/language.oop5.properties.php

Upvotes: 0

Related Questions