1

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)
    ?>
PeanutsMonkey
  • 6,919
  • 23
  • 73
  • 103
  • Not sure why you define the function as `function User($firstname, $lastname, $email)` and you are not using those variables anywhere. – morgar Apr 29 '11 at 21:22
  • 1
    *(related)* [Learning PHP class](http://stackoverflow.com/questions/2206387/learning-php-class/2206835#2206835) and [Is this correct object oriented programing in php?](http://stackoverflow.com/questions/5329664/is-this-correct-object-oriented-programing-in-php/5333415#5333415) – Gordon Apr 29 '11 at 21:25
  • 3
    There are so many things wrong with what you are doing, you really need to actually learn and understand OO programming with PHP before try to tackle a project like this. – andrewtweber Apr 29 '11 at 21:26
  • We can't debug your code when you don't include the *actual* code you're running. Instead, you have included an abbreviated version, that contains syntax errors, in which the only lines that appear are those which **you** think are helpful. – webbiedave Apr 29 '11 at 21:44
  • @Peanuts, we meet again! Remember in [your last question](http://stackoverflow.com/questions/5829099/php-web-service/5829367#5829367), when I mentioned that PHP is a simple language for simple things? PHP OO doesn't qualify as a simple thing, especially given how much it differs from OO in other languages, like C#. A few of the mistakes in your code here seem to be reflections of OO in other languages. Consider sticking to procedural code until you're more familiar with PHP. – Charles Apr 29 '11 at 21:54

3 Answers3

2

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']);
DaOgre
  • 2,080
  • 16
  • 25
  • Good point about var being depreciated @morgar. I updated the sample code to reflect. – DaOgre Apr 29 '11 at 21:32
  • @DaOgre - Thanks but I don't quite follow when you say **User operates like a regular function** as well as when you say **class variables**. I presume that you mean data members refer to **class variables**. I have no idea what is the difference between public, private or protected and when you must use them. Are you able to give me examples when I must use each of them? Also having had a look at your code what do you mean by Input Sterilization? – PeanutsMonkey Apr 29 '11 at 22:57
  • continued - `class myform { public $firstname, $lastname, $email; function setUsername($value) { //Add Form Input Sterilization here then $this->firstname = $value; } } ` and the main code `$validate = new myform; $validate->setUsername($_POST['fname']); ` In the example above I take it I can write the code as `$validate = new myform; $validate->setUsername(($_POST['fname'] ? $_POST['fname'] : ''); ` – PeanutsMonkey Apr 29 '11 at 22:57
  • You're calling the class function "User" in your original code but it requires 3 variables be passed, and just like any other function you'ld use, those variables need to be defined. If you try to call a function without all the required functioned being defined you'll get an error. Try the code I linked with public variables, without getting into it too deeply they'll function the most like the variables you're looking for. By input sterilization check this link: http://stackoverflow.com/questions/1129920/what-is-the-theoretical-best-way-to-sterilize-inputs. – DaOgre Apr 29 '11 at 23:20
  • @DaOgre - I understand that variables must be defined however what I don't understand and please forgive my noobness is the following; 1. When do I declare the variables as protected, private or protected? Are there specific times? 2. I now understand what you meant by sterilization or data sanitization however do I code as follows `class myform { public $firstname, $lastname, $email; function setUsername($value) { //Add Form Input Sterilization here then $this->firstname = htmlentities($value); } }` – PeanutsMonkey Apr 30 '11 at 02:38
  • Yeah your code looks correct there. Are you asking in what instance do you use different variables? You should be able to read up on this more in depth with a google search. Such as: http://www.java-samples.com/showtutorial.php?tutorialid=913. If you're just asking when in the code you make that declaration, just once when you declare the variable itself. – DaOgre May 02 '11 at 16:42
0

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

morgar
  • 2,339
  • 17
  • 16
0

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)
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
user719852
  • 131
  • 1
  • 3
  • 8