简体   繁体   English

解析错误:T_PAAMAYIM_NEKUDOTAYIM

[英]Parse error: T_PAAMAYIM_NEKUDOTAYIM

I got this simple cache class setted up as a library in the Codeigniter: 我在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');
    }

}

I want to use it now, so in the controller I'm using this (I'm loading the library via autoload.php ): 我现在想使用它,因此在控制器中,我正在使用它(我通过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()));
    }
}

And then I'm getting this error: 然后我得到这个错误:

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in [..]

I guess its something related to the double dots and the static function, but I'm a newbie with the classes, so whats the problem? 我猜它与双点和静态函数有关,但是我是此类的新手,那么问题出在哪里呢?

Your mixing an instance call with static calls. 您将实例调用与静态调用混合在一起。

$cache = $this->easy_cache;
!$cache::is_cached

should be.. 应该..

!$cache->is_cached();

same with.. 与...相同

$cache::store

You're either working within the context of an object (using $this), or perform a static call (using ::). 您要么在对象的上下文中工作(使用$ this),要么执行静态调用(使用::)。 You can't mix them. 你不能混合他们。

You should use statics calls ( ::someMethod() ) with the class name, not a class instance. 您应该使用带有类名而不是类实例的静态调用( ::someMethod() )。

Since all methods of Easy_cache are static, you should do 由于Easy_cache所有方法都是静态的,因此您应该

Easy_cache::is_cached()
Easy_cache::store()

instead of 代替

$cache::is_cached()
$cache::store()

BTW, are you sure that this comes from CodeIgniter codebase? 顺便说一句,您确定这来自CodeIgniter代码库吗? This mixes static and dynamic context: 这混合了静态和动态上下文:

static function Easy_cache()
{
    if ($this->expire_after == '')
    {
         $this->expire_after = 300;
    }
}

IMO, class Easy_cache should be used like you tried to, but: IMO,应该像您尝试的那样使用Easy_cache类,但是:

  • use -> instead of :: for method calls 使用->代替::进行方法调用
  • remove all static keywords in the methods definitions 删除方法定义中的所有static关键字
  • (optional but recommended) rename Easy_cache() method to __construct() (可选,但建议使用)将Easy_cache()方法重命名为__construct()

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

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