2015-02-06 34 views
0

我是PHP程序設計師

我有類ActionDTO在ActionDTO.php

class ActionDTO { 
    private $totalCount; 

    public function getTotalCount(){ 
     return $this->totalCount; 
    } 

    public function setTotalCount($totalCount){ 
     $this->totalCount = $totalCount; 
    } 
} 

和Main.php

$a = new ActionDTO();   // create new instance 
$b = [$a];     // add it to array 
foreach($b as $value){  // loop all item in $b 
    print_r($value);   // show info of object ActionDTO, it right! 
    echo $value::getTotalCount(); // ** 
} 

,但在**,我給

PHP Fatal error: Using $this when not in object context in /path/to/ActionDTO.php on line 35 

我不知道,爲什麼錯誤!

+2

'$值 - > getTotalCount();'使用''::調用函數靜態 – 2015-02-06 10:16:04

+0

嘗試回聲$值 - > getTotalCount() – 2015-02-06 10:16:13

+0

感謝你:D,它解決了 – 2015-02-06 10:21:12

回答

0

您正在調用與::功能,但它不是靜態方法。因此使用下面

$a = new ActionDTO();   // create new instance 
$b = [$a];     // add it to array 
foreach($b as $value){  // loop all item in $b 
    print_r($value);   // show info of object ActionDTO, it right! 
    echo $value->getTotalCount(); // ** 
} 

希望這段代碼可以幫助您

相關問題