Methods vs. Functions

I’m working on a PHP based task manager which will consist of a form allowing users to add new tasks, another to edit tasks, and some operation for deleting tasks. In addition, it will frequently loop through a database and display the most recent tasks output in an HTML table.

My questions is whether or not the above features are best coded in individual PHP functions (e.g. AddTask(), DeleteTask(), EditTask(), etc.) or using classes, objects, and other OOP principles. Should common elements like a header, navigation, footer, etc. be placed in classes instead? It seems like writing individual functions and creating classes/objects/methods are very similar in what they do – helping to repeat and modularize parts of an application. I’m having trouble distinguishing which elements of an app are better coded using certain methods and any help is appreciated. Thanks :D!

The common elements like header, footer and navigation bar could simply be plain html files (with .php extensions) and inserted into their respective locations in each web page using include()

regarding working out the functionality of your website, the first thing you should do is storyboard your website.

after you have done that, then 1 way of implementing it, depending on your storyboard, is

  1. create an input form to create a task (createTaskForm.php)

  2. createTaskForm.php sends all the form data to a php script, createTask.php, which then processes the form data to create the task in the database

  3. you can apply the same concept for editing tasks by creating a editTaskForm.php which drives editTask.php

  4. you could also have a form, displayTasks.php, which displays all the tasks and relevent task info in a table with a row for each task. For each task you could add a link that takes you to editTaskForm.php with that taskId as input for ediTaskForm.php and a checkbox which sends the taskId to deleteTasks.php to delete that task if it is checked when the “Delete selected tasks” button is clicked.

Thanks for that info and for pointing me in the right direction. I’m still having trouble understanding the practical uses of classes, methods , etc. I get why they’re useful but just not when you’d use them instead of functions. Couldnt I create a task class that had methods for adding, updating or deleting records depending on which values were passed to it?

Methods are functions. A class is a datatype / data structure that allows functions to be a part of its members. Think Struct in C language. The original C did not allow functions to be part of it. C++ support standard structs like C, but also added functions to be part of the structure too. They are called methods when used like this.

C went on to allow functions to be methods of structs like the other data types so the main difference between C Structs and C++ Classes is visibility. IE The Class can have private, public and protected access rights to members of the class. Php 5 classes work in this way too.

As to whether or not your code should use functions or classes I would really just determine how much time you are going to put into maintaining the code. If this is something that will grow over time then classes. If this is a one-off piece of glue then functions can work just fine.

Personally, classes offer much better longevity. I’d even look into using a MVC framework so you can see a real word example of how classes make your life easier.

the idea behind a class is that it is a standalone piece of software that behaves like a real life object (having properties and behaviours). The class should be transferable from one application to another without its code having to be modified in any way.

You could create a Task class for your application but I’m not sure it would be easy to make it totally generic so it could be transferable to say another developer’s “task manager” website without needing to be modified to suit.
If your class had to modified, that would defeat the purpose of a class, hence my suggestion of having largely pairs of form web pages driving a corresponding script to perform a task like adding or deleting tasks.

Now within each script, like addTask.php, you could include a class to validate the form data (FormValidatorClass.php).

FormValidatorClass.php could be used to validate inputs like task name, task description etc for invalid characters or whatever.