Php 原因是什么:不在对象上下文中使用$this

Php 原因是什么:不在对象上下文中使用$this,php,object,Php,Object,致命错误:在第29行的plugins/newsactions/controller/newsactioncontroller.php中不在对象上下文中时使用$this 我可以看到哪里出错,第29行是这一行: $sqlFetch = $this->model->getAllNewsArticlesByDate(); 下面我已经提交了我的代码,有人能解释一下吗 <?php require_once("plugins/newsarticles/model/NewsArticleDB

致命错误:在第29行的plugins/newsactions/controller/newsactioncontroller.php中不在对象上下文中时使用$this

我可以看到哪里出错,第29行是这一行:

$sqlFetch = $this->model->getAllNewsArticlesByDate();
下面我已经提交了我的代码,有人能解释一下吗

<?php
require_once("plugins/newsarticles/model/NewsArticleDB.php");
class NewsArticleController
{
    private $model;

    public function __construct()
    {
        $this->model = new NewsArticleDB();
    } //end constructor

        /**
    *Grab all the News Articles using the function
    *Filter by date
    *return the most recent 10
    */  
    function newsArticleHome()
    {
        //Grab the News Articles by date
        $sqlFetch = $this->model->getAllNewsArticlesByDate();
        foreach ($sqlFetch as $row)
        {

            //Insert posts into an object array
            $objNewsArticle[] = new NewsArticle($row['newsId'],$row['newsTitle'], $row['newsPreview'], $row['newsDisplayPicture'],  $row['newsContet']." ".$row['newsCategories'], $row['newsSubmissionDate']); 
        }

        //Count the array incase it's less than 10 posts
        //If it's more than 10, then set to 10, if it's less then set to x
        if(count($objNewsArticle) > 10)
        {
           $tempObjectCount = 10;
        }
        else
        {
            $tempObjectCount = count($objNewsArticle);
        }
        //Store them in an array for output
        for($tempLoopNumber = 0; $tempLoopNumber<$tempObjectCount; $tempLoopNumber++)
        {
            $recentNewsArticles[$tempLoopNumber] = $objNewsArticle[$tempLoopNumber];
        }
        return $recentNewsArticles;
    }

    function newsArticleId($id)
    {
        //Grab the Posts by date
        $sqlFetch = $this->model->getAllNewsArticlesByDate();
                $tempOptions = $sqlFetch->fetchAll();
        $tempOpNumber = count($tempOptions);


        for($tempNewsArticleNumber = 0; $tempNewsArticleNumber<$tempOpNumber; $tempNewsArticleNumber++)
        {
            if($tempOptions[$tempNewsArticleNumber]['newsId'] == $id)
            {

                $singlePost = $tempOptions[$tempNewsArticleNumber];
            }
        }
        return $singlePost;
    }

} //end class
?>

NewsArticleController::newsArticleHome()
正在调用函数,就好像它是静态函数一样。您需要创建类的实例

$nacont = new NewsArticleController();
$nacont->newsArticleHome();

你怎么调用这个函数?哦,对不起,哈哈,我可能应该加上这个。它位于另一个名为testing的文件中,下面是其他文件的完整内容,包括_once(“plugins/newsactions/controller/newsactioncontroller.php”);呼应“出去”;echo NewsArticleController::newsArticleHome();好了。您将其称为静态方法。静态方法没有
$this
。如果您想使用
$this
,则只能使用
$obj->method()
-键入调用。是的,谢谢,当我意识到我得到了解决方案时,我删除了注释,只有一半人阅读了答案:pSo等等,这应该可以从测试文件中找到是吗?echo$NewsArticleController->newsArticleHome();前提是我将其定义为对象。
$nacont = new NewsArticleController();
$nacont->newsArticleHome();