0

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; 
        }
    }
}
Community
  • 1
  • 1
14wml
  • 4,048
  • 11
  • 49
  • 97
  • 1
    _"I'm not sure what syntax error..."_ - Step one when debugging, check your error log for a proper error message. – M. Eriksson Feb 23 '17 at 15:58
  • it is shown some message error? – Jose Rojas Feb 23 '17 at 15:59
  • What did you expect, whats going wrong? – JustOnUnderMillions Feb 23 '17 at 15:59
  • It could be that the if statements in your `task_from_form()` doesn't validate to true, and therefore not returning an instance of your class, which you're trying to use later on. This: `$task->is_empty()` will break your code if the function returned null instead of a class instance – M. Eriksson Feb 23 '17 at 16:00
  • @MagnusEriksson where can I see this error log? I'm using brackets for my text editor and MAMP to see my website, which I'm viewing on Chrome – 14wml Feb 23 '17 at 16:01
  • To get the errors in MAMP: http://stackoverflow.com/questions/8641383/how-can-i-get-mamp-to-tell-me-what-went-wrong-with-php-code – M. Eriksson Feb 23 '17 at 16:01
  • @MagnusEriksson It seems that I'm getting `[23-Feb-2017 16:49:31 Europe/Berlin] PHP Fatal error: Call to a member function is_empty() on null in /Applications/MAMP/htdocs/p2/index.php on line 116` – 14wml Feb 23 '17 at 16:04
  • Quick fix can be `if(is_object($task)){$is_empty = $task->is_empty();} else {$is_empty =true;}` instead of just `$is_empty = $task->is_empty();` – JustOnUnderMillions Feb 23 '17 at 16:04
  • @JustOnUnderMillions Question: is the error saying `$task` is null? – 14wml Feb 23 '17 at 16:05
  • @MagnusEriksson why might task be null? is it b/c it only returns in the if statement? – 14wml Feb 23 '17 at 16:08
  • You only return the class instance if all your if statements validates to true. If any of those conditions are false, your function won't return anything, which is the same as NULL. NULL = Lack of anything. – M. Eriksson Feb 23 '17 at 16:11
  • Yes, I noticed that, removed the comment :) – M. Eriksson Feb 23 '17 at 16:11
  • @MagnusEriksson mmm I see. I guess I'll add another if block to check `is_null` before using `$task` – 14wml Feb 23 '17 at 16:12
  • Yes, you'll need to do that. Check the suggestion in @JustOnUnderMillions comment. – M. Eriksson Feb 23 '17 at 16:14
  • @MagnusEriksson Whew, got that working finally! Thanks for your help! – 14wml Feb 23 '17 at 16:17

1 Answers1

0

Thanks to @MagnusEriksson & @JustOnUnderMillions comment I realized that $task = task_from_form() was returning null since the return statement was nested in an if statement. So if the if statement did not evaluate to true the task_from_form() would return null. And then I would call the instance method is_empty() on a null $task. So I used is_null($task) to check for null before manipulating $task.

I also found out how to find errors using MAMP! Which was honestly the main problem b/c I didn't know how to do that before so debugging was quite difficult

Fixed code

                //If valid form elements & not a duplicate, add it to data file
            if($valid_title && $valid_note && $valid_date){
                $task = task_from_form();
                //Make sure task is not null
                if(!is_null($task)){
                    //Don't add duplicates
                    if(!$task->is_empty() && !in_array($task, $tasks)){
                        //write task to file
                        write_file($filename, $task);
                        //add task to gloabl var tasks
                        $tasks[] = $task;
                    }
                }
            }
Community
  • 1
  • 1
14wml
  • 4,048
  • 11
  • 49
  • 97