【问题标题】:Fatal error: Call to a member function Function() on boolean致命错误:在布尔值上调用成员函数 Function()
【发布时间】:2017-01-06 11:57:44
【问题描述】:

我编写了简单的插入数据代码,但每当使用!empty($author) 时,它都会给我这个错误Fatal error: Call to a member function Create() on boolean,但我删除了对!empty($author) 的检查,所以它工作正常。我真的不明白这是给出这个错误以及它的含义。

这是我的代码注释类

 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;

    }
}

这是我的表单提交代码

// 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}");
        }
    }

【问题讨论】:

  • 检查$_POST['name']是否有空字符串。你以name 提交什么?
  • 我正在提交作者姓名。 input text 如果将其留空,则默认情况下应在其中写入Anonymous,但事实并非如此。它在其中插入空白字段

标签: php post form-submit


【解决方案1】:

我认为 public static function Make 的关系( DatabaseObject )在“Make”方法的结果上调用“Create”方法。如果条件失败,您将返回 FALSE。然后 DatabaseObject 调用方法 Create on FALSE - 出现错误!如果必须调用 Create 方法,则最好抛出异常而不是返回 FALSE 或空对象。

【讨论】:

  • 实际上伙计,它之前将数据完美地插入到数据库中,但是当我使用!empty($author) 之类的名称检查时,它开始出现此错误,即使它不会出现其他情况,我正在填写所有字段,所以它仍然存在读取其中的其他条件。
  • 那么请告诉我 var_dump( !empty($photograph_id) && !empty($author) && !empty($body) );
  • [id] => [author] => Hamza [comment] => I love it [created] => 2017-01-06 12:34:27 [photograph_id] => 26
  • 它正在工作,但它没有获得$author 的默认值,这是我在方法上给出的
  • 可能,作者姓名为“0”(零) - 空将设置为真,因为字符串“0”也为空。考虑以下代码:$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));
【解决方案2】:

你的问题是你的方法签名,

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

默认参数就是这样工作的。如果您发送string(0) ""$author 参数将采用空字符串值而不是'Anonymous'

你有几个选择,

要么更改参数的顺序并将 $author 作为最后一个参数,要么在没有提交作者姓名的情况下使其成为可选参数,或者您可以用 'Anonymous' 替换空的作者姓名,将其作为类定义中的某个类常量。

另外,这个question 可能会有所帮助。

【讨论】:

    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    相关资源
    最近更新 更多