Cyc*_*one 3 php codeigniter class
我收到这个致命错误消息:Using $this when not in object context.
此类在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);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在通过它初始化它autoload.php.它抛出错误的行:
file_put_contents( $this->app . $key.'.cache', $value);
我的问题在哪里?
您不能$this在静态方法中使用.该变量$this仅适用于类方法,因为它们接收调用该方法的对象.
这就是"当不在对象上下文中"时意味着:没有对象传递给该静态方法,因为它是静态的.静态方法是类的一部分,而不是使用该类实例化的对象的一部分.