duan6832168 2012-04-03 15:04
浏览 70
已采纳

解析错误:T_PAAMAYIM_NEKUDOTAYIM

I got this simple cache class setted up as a library in the 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):

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?

  • 写回答

2条回答 默认 最新

  • duanguangsong2380 2012-04-03 15:10
    关注

    You should use statics calls (::someMethod()) with the class name, not a class instance.

    Since all methods of Easy_cache are static, you should do

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

    instead of

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

    BTW, are you sure that this comes from CodeIgniter codebase? 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:

    • use -> instead of :: for method calls
    • remove all static keywords in the methods definitions
    • (optional but recommended) rename Easy_cache() method to __construct()
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?