1

I wrote simple insert data code but whenever use !empty($author) it give me this error Fatal error: Call to a member function Create() on boolean but i remove check for !empty($author) so then it's work fine. I really don't understand this is giving this error and what mean of it.

Here is my code Comment Class

 class Comment extends DatabaseObject{
    // Attributes
    protected static $TableName = 'comment';
    protected static $DBFields = array('id','author','comment','created','photograph_id');
    public $id;
    public $author;
    public $comment;
    public $created;
    public $photograph_id;

    // Create Comment
    public static function Make($photograph_id,$author='Anonymous',$body=''){
        if(!empty($photograph_id) && !empty($author) && !empty($body)){
            $Comment = new Comment();
            $Comment->author = $author;
            $Comment->comment = $body;
            $Comment->photograph_id = (int)$photograph_id;
            $Comment->created = date("Y-m-d H:i:s",time());
            return $Comment;
        }else{
            return FALSE;
        }

    }
    // Find Comment Related Picture
    public static function CommentOfPicture($photograph_id){
        global $db;
        $Comment = static::FindByQuery("SELECT * FROM ".static::$TableName." WHERE `photograph_id`='".$db->EscapeValue($photograph_id)."' ORDER BY created ASC");
        return $Comment;

    }
}

Here is my code of Form Submission

// Comment Submit
    if(isset($_POST['submit'])){
        $Name = trim($_POST['name']);
        $Body = trim($_POST['comment']);
        if(!empty($Body) || !empty($Name)){
            $Comment = Comment::Make($Photo->id,$Name,$Body);
            if($Comment->Create()){
                $Session->MSG("Success: Comment is submit, awaiting for approval");
                RedirectTo("photo.php?id={$Photo->id}");
            }else{
                $Session->MSG("Danger: Something is Wrong");
                RedirectTo("photo.php?id={$Photo->id}");
            }           
        }else{
            $Session->MSG("Danger: Comment is empty");
            RedirectTo("photo.php?id={$Photo->id}");
        }
    }
vaibhavmande
  • 1,115
  • 9
  • 17

2 Answers2

1

I think that a relation ( DatabaseObject ) of public static function Make calls the method "Create" on the result of the "Make" method. You're returning FALSE if the condition fails. Then the DatabaseObject calls the method Create on FALSE - with the error! It will be better, if you'll throw an exception instead of returning FALSE or an empty object, if the Create method has to be called.

Lukas
  • 201
  • 1
  • 4
  • Actually buddy it's inserting data perfectly into the database before but when i use check for name like `!empty($author)` it's start giving this error even it's not going to else condition I'm filling all field so it's still reading else condition inside it. – Muhammad Hamza Nisar Jan 06 '17 at 12:23
  • Then please tell me the result of var_dump( !empty($photograph_id) && !empty($author) && !empty($body) ); – Lukas Jan 06 '17 at 12:29
  • `[id] => [author] => Hamza [comment] => I love it [created] => 2017-01-06 12:34:27 [photograph_id] => 26` – Muhammad Hamza Nisar Jan 06 '17 at 12:35
  • it's working but it's not getting default value of `$author` which is i'm giving on method – Muhammad Hamza Nisar Jan 06 '17 at 12:50
  • It could be, that the author name is "0" (zero) - empty will set it true as the string "0" is also empty. Consider this code: `$photograph_id = 1; $author = 'aaaaa'; $body = 'bbbb'; var_dump( !empty($photograph_id) && !empty($author) && !empty($body)); $photograph_id = 1; $author = '0'; $body = 'bbbb'; var_dump( !empty($photograph_id) && !empty($author) && !empty($body));` – Lukas Jan 06 '17 at 13:47
1

Your problem is your method signature,

public static function Make($photograph_id,$author='Anonymous',$body='')

The default argument is going to work like that. If you send string(0) "", the $author parameter is going to take empty string value and not 'Anonymous'

You have few options,

Either change the order of your arguments and make $author as last argument ot make it optional if no author name submitted or you can substitue empty author name with 'Anonymous' taking it as class constant somewhere in the class definition.

Also, this question might help.

Community
  • 1
  • 1
vaibhavmande
  • 1,115
  • 9
  • 17