是什么导致这种情况:在不在对象上下文中时使用$ this

时间:2014-07-24 19:26:32

标签: php object

  

致命错误:在第29行的插件/ newsarticles / controller / NewsArticleController.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
?>

我从另一个名为testing的文件中调用它:

include_once("plugins/newsarticles/controller/NewsArticleController.php");
echo "out";
echo NewsArticleController::newsArticleHome();

1 个答案:

答案 0 :(得分:2)

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

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