Search code examples
phphttp-redirectverification

ensuring PHP good coding practice with functions?


I'm relatively new to PHP, and I want to ensure I code as efficiently and as correctly I can - specifically, I'd like to ask about how functions should be implemented with each other and how large and how many tasks they each should do under optimal conditions.

Example

For the sake of an example (and possibly something I will need to implement), say there is link to a page which reads "forgotten your username/password?", upon clicking this link, they are brought to a page forgot.php, which has two forms on it:

1) If they've forgotten their password, they enter their username, and a link to a reset form is sent to their email address.

2) And another is if they've forgotten their username, they enter their email address, and this is sent to them via their email address .

Upon clicking submit, the relevant form is $_POST'ed to a new page called forgotsubmit.php, which checks if it is being accessed legitimately through an if statement:

//loggedIn() is a previously defined function

if (!loggedIn() && isset($_POST['forgotUsername']) || isset($_POST['forgotPassword'])) {
    header('Location: index.php');
    exit();
}

Otherwise, the page will run normally, and that is: start a function(s), and display a message telling the user to check their email. How I would implement/create the function(s) necessary for this to occur.

Possibility One - 1 function

Should there be a single, large, function that is called that can handle both forgotten passwords and usernames, through an if statement inside the function, then create a query fetching the relevant fields of data from a MySQL databse, organize and then strip_tags() all the data, send an email containing the necessary information, and then echo a success/failure back depending on what form was chosen and whether the code completed without errors?

Possibility Two - 2 functions

Have an if statement on forgotsubmit.php which determines which function to run - one to fetch a username, and another to reset the password, each doing what was described in possibilty one.

Possibility Three - a handful of functions

The same as possibility two, except a separate sendMail() function could send out the email, another function could handle the MySQL queries, etc.

Does calling too many functions slow performance? Is the (if any) performance loss made up by the time cost of not having to code sending mail twice, for example?

Possibility Four - dozens of functions, no functions, something else entirely?

Could I be using something better for a task like this? Would multiple functions be appropriate? I have heard of classes, but haven't looked into them, would they be appropriate?

Summary

I hope I have made myself concise enough. As I understand (or have read), the job of a function is to 'make repetitive processes simple', perhaps one of these possibilities would fulfill that task. How would you code it? Thanks.


Solution

  • It completely depends on the task on your hand. Its always better to have some functions like do_this(), do_that() instead do_all_stuff().

    For me I follow a general rule. Each time I end up need a code block I wrote earlier, I convert it to a function. If you follow this rule, you'll end up creating lots of re-usable functions. Also the code will automatically be refactoring.