2014-07-24 26 views
-1

Fatal error: Using $this when not in object context in plugins/newsarticles/controller/NewsArticleController.php on line 29

我能看到它示數,第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(); 
+2

你打電話*這個功能? –

+0

哦,對不起,哈哈,我應該補充一點。 它在另一個文件命名測試下面是其他文件的全部內容 include_once(「plugins/newsarticles/controller/NewsArticleController.php」); 回聲「out」; 回聲NewsArticleController :: newsArticleHome(); –

+0

你去了。你將它稱爲靜態方法。靜態方法沒有'$ this'。如果你想使用'$ this',你只能使用'$ obj-> method()'類型的調用。 –

回答

2

NewsArticleController::newsArticleHome();調用函數,就好像它是一個靜態函數。你需要創建一個類的實例。

$nacont = new NewsArticleController(); 
$nacont->newsArticleHome(); 
相關問題