如何访问方法中的类属性


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(....);

另一个修复方法是使属性也是静态的,这样您就有了public static $img...