[英]How to access class property in method
我有以下代碼:
class FanClub_Banner
{
public $img = 'http://www.example.com/museum/images/logo_ver_250.png';
public static function banner_me(array $widget, $positionCode, array $params, XenForo_Template_Abstract $renderTemplateObject)
{
return '<img src="'. $this->$img . '" width="250" height="250" alt="Museum">';
}
}
我得到錯誤:
Fatal error: Using $this when not in object context in C:\public_html\comunidad\library\FanClub\Banner.php on line 8
如何解決?
謝謝
您的靜態方法無法訪問您的Class屬性,一個簡單的解決方法是從public static function
刪除該static
,然后在您要使用此方法的地方執行FanClub_Banner fcBanner = new FanClub_Banner(); fcBanner->banner_me(....);
FanClub_Banner fcBanner = new FanClub_Banner(); fcBanner->banner_me(....);
另一個解決方法是將屬性也設置為靜態,因此您將擁有public static $img...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.