致命错误:当不在对象上下文中时使用$ this [英] Fatal Error: Using $this when not in object context

查看:379
本文介绍了致命错误:当不在对象上下文中时使用$ this的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到这个致命错误消息:在不在对象上下文中使用$ this
此类设置为CodeIgniter中的库。

I'm receiving this fatal error message: Using $this when not in object context. This class is setted up as a library in the CodeIgniter.

这是我的课程:

class My_class {

    function __construct()
    {
            $this->app = base_url('application') . '/cache/';
            if ($this->expire_after == '')
            {
                $this->expire_after = 300;
            }
    }

    static function store($key, $value)
    {
        $key = sha1($key);
        $value = serialize($value);
        file_put_contents( $this->app . $key.'.cache', $value);
    }
}

我通过 autoload.php
这行抛出的错误在:

I'm initializing it via autoload.php. The line it is throwing the error at:

file_put_contents($ this-> app。$ key。'cache' ,$ value);

file_put_contents( $this->app . $key.'.cache', $value);

我的问题在哪里?

推荐答案

$ this 将不会在静态函数中可用。您可能希望在静态函数中重新创建 $ app

$this will not be available in a static function. You'll probably want to re-create $app within the static function:

static function store($key, $value)
{
    $app = base_url('application') . '/cache/';
    $key = sha1($key);
    $value = serialize($value);
    file_put_contents( $app . $key.'.cache', $value);
}

我不太确定你要在您的应用程序的上下文,但您可能不需要 static 方法。

I'm not quite sure what you're trying to do in the grand context of your application, but you may not even need a static method in the first place.

这篇关于致命错误:当不在对象上下文中时使用$ this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆