Problem
I have this Task
class in php:
class Task{
public $title;
public $due_date;
public $priority;
public $course;
public $note;
function __construct($title, $due_date, $priority, $course, $note) {
$this->title = $title;
$this->due_date = $due_date;
$this->priority = $priority;
$this->course = $course;
$this->note = $note;
}
public function is_empty(){
return ($this->title === '' || $this->due_date === '' || $this->priority === '' || $this->course ==='' || $this->note ==='');
}
}
But when I try to use is_empty()
, it doesn't work (& stops all functionality below):
//If valid form elements & not a duplicate, add it to data file
if($valid_title && $valid_note && $valid_date){
$task = task_from_form();
//Don't add duplicates or tasks with empty elements
echo "work please"; //prints
$is_empty = $task->is_empty();
echo "$is_empty"; //doesn't print
if(!$is_empty && !in_array($task, $tasks)){
//write task to file
write_file($filename, $task);
//add task to gloabl var tasks
$tasks[] = $task;
}
}
I'm not sure what syntax error I'm making, and I'm sure it's something stupid, so any advice would be greatly appreciated!
Code
The task_from_form()
function (but I know this works b/c I've used it before without calling $task->is_empty()
):
//Return task from form elements
function task_from_form(){
if(isset($_POST['submit']) && isset($_POST['title']) && isset($_POST['note'])){
if($_POST['title'] !== '' && $_POST['note'] !== ''){
$title = $_POST['title'];
$note = $_POST['note'];
$title_trim = trim($title);
$note_trim = trim($note);
$title_html = htmlentities($title_trim);
$note_html = htmlentities($note_trim);
$due_date = $_POST['due-date'];
$priority = $_POST['priority'];
$course = $_POST['course'];
$course_space = str_replace("-", " ", $course);
$task = new Task($title_html, $due_date, $priority, $course_space, $note_html);
return $task;
}
}
}