[英]Parse error: T_PAAMAYIM_NEKUDOTAYIM
我在Codeigniter中将这个简单的缓存类设置为库:
<?php
class Easy_cache {
static public $expire_after;
static function Easy_cache()
{
if ($this->expire_after == '')
{
$this->expire_after = 300;
}
}
static function store($key, $value)
{
$key = sha1($key);
$value = serialize($value);
file_put_contents(BASEPATH.'cache/'.$key.'.cache', $value);
}
static function is_cached($key)
{
$key = sha1($key);
if (file_exists(BASEPATH.'cache/'.$key.'.cache') && (filectime(BASEPATH.'cache/'.$key.'.php')+$this->expire_after) >= time())
return true;
return false;
}
static function get($key)
{
$key = sha1($key);
$item = file_get_contents(BASEPATH.'cache/'.$key.'.cache');
$items = unserialize($item);
return $items;
}
static function delete($key)
{
unlink(BASEPATH.'cache/'.sha1($key).'.cache');
}
}
我现在想使用它,因此在控制器中,我正在使用它(我通过autoload.php
加载库):
class Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$cache = $this->easy_cache;
if ( !$cache::is_cached('statistics') )
{
$data = array('data' => $this->model_acc->count());
$cache::store('server_statistics', $data);
}
else
$data = array('this' => 'value');
$this->load->view('main', array('servers' => $this->servers->get()));
}
}
然后我得到这个错误:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in [..]
我猜它与双点和静态函数有关,但是我是此类的新手,那么问题出在哪里呢?
您将实例调用与静态调用混合在一起。
$cache = $this->easy_cache;
!$cache::is_cached
应该..
!$cache->is_cached();
与...相同
$cache::store
您要么在对象的上下文中工作(使用$ this),要么执行静态调用(使用::)。 你不能混合他们。
您应该使用带有类名而不是类实例的静态调用( ::someMethod()
)。
由于Easy_cache
所有方法都是静态的,因此您应该
Easy_cache::is_cached()
Easy_cache::store()
代替
$cache::is_cached()
$cache::store()
顺便说一句,您确定这来自CodeIgniter代码库吗? 这混合了静态和动态上下文:
static function Easy_cache()
{
if ($this->expire_after == '')
{
$this->expire_after = 300;
}
}
IMO,应该像您尝试的那样使用Easy_cache
类,但是:
->
代替::
进行方法调用 static
关键字 Easy_cache()
方法重命名为__construct()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.