dsfsdfsd34324 2017-10-14 11:59
浏览 43
已采纳

PHP OOP,未捕获错误:在字符串[duplicate]上调用成员函数setAuthor()

This question already has an answer here:

I use PHP version 7 and i get always this error, when i creat a news article:

Fatal error: Uncaught Error: Call to a member function setAuthor() on string in D:\xampp\htdocs\entwicklung\tools\adminlogin\lib\mod ews ewsForm.php:14 Stack trace: #0 {main} thrown in D:\xampp\htdocs\entwicklung\tools\adminlogin\lib\mod ews ewsForm.php on line 14

Here i creat my object and would save the article in my database:

if(isset($_POST["publish"]))
{
    $news = new News();
    $title = $news->setTitle($db, $_POST["ueberschrift"]);
    $news =  $news->setNews($db, $_POST["artikel"]);
    $author = $news->setAuthor($db, $_POST["autor"]); //This is line 14

    News::newsArticleCreate($db, $title, $news, $author);

}

This is my setter-method for the author:

    function setAuthor($db, $author)
    {
        if(!empty($author))
        {
            $author = mysqli_real_escape_string($db, $author);
            return $this->author = trim($author);
        }
        else
        {
            return false;
        }
    }

What is my mistake?

</div>
  • 写回答

1条回答 默认 最新

  • dongxuxian1123 2017-10-14 12:08
    关注

    Fatal error: Uncaught Error: Call to a member function setAuthor() on string

    The error says that you are trying to call setAuthor on a string ( $news ) rather than on an object of News Class. If you replace the following line:

    $news =  $news->setNews($db, $_POST["artikel"]);
    

    with

    $news->setNews($db, $_POST["artikel"]);
    

    It should work fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?