Reputation: 601
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}");
}
}
Upvotes: 1
Views: 6129
Reputation: 1111
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.
Upvotes: 1
Reputation: 201
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.
Upvotes: 1