简体   繁体   English

如何在方法中访问类属性

[英]How to access class property in method

i have the following code: 我有以下代码:

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">';
    }
}

and i get the error: 我得到错误:

Fatal error: Using $this when not in object context in C:\public_html\comunidad\library\FanClub\Banner.php on line 8

How to fix it? 如何解决?

Thanks 谢谢

Your static method cannot access your Class property, a simple fix would be to remove the static from public static function and where you want to use this method you'd do FanClub_Banner fcBanner = new FanClub_Banner(); fcBanner->banner_me(....); 您的静态方法无法访问您的Class属性,一个简单的解决方法是从public static function删除该static ,然后在您要使用此方法的地方执行FanClub_Banner fcBanner = new FanClub_Banner(); fcBanner->banner_me(....); FanClub_Banner fcBanner = new FanClub_Banner(); fcBanner->banner_me(....);

Another fix would be to make the property static as well so you'd have public static $img... 另一个解决方法是将属性也设置为静态,因此您将拥有public static $img...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM