5

我想扩展这个类(我下载的)以满足我自己的需要。当我运行调用这个类时,我收到一个错误,抱怨=构造函数中有意外。

define("HIT_OLD_AFTER_SECONDS", 4 * 7 * 24 * 3600);
class PHPCount extends DatabaseObject {

    protected static $table_name = "hits";
    protected static $db_fields = array('pageid','isunique', 'hitcount','english_id');
    public $pageid;
    public $isunique;
    public $hitcount;
    public $english_id;
    public static $article_id;

    function __construct(){
        return  PHPCount::article_id = PHPCount::get_article_id();
    }
    public static function set_article_id($articleid){
        PHPCount::article_id = $articleid;
    }
    public static function get_article_id(){
        return PHPCount::article_id;
    }
    public static function AddHit($pageID, $visitorID){

        HitTest::Cleanup();
        self::CreateCountsIfNotPresent($pageID);
        if(HitTest::UniqueHit($pageID, $visitorID)){
            self::CountHit($pageID, true);
            HitTest::LogHit($pageID, $visitorID);
        }
        self::CountHit($pageID, false);
    }

    /*
     * Returns (int) the amount of hits a page has
     * $pageID - the page identifier
     * $unique - true if you want unique hit count
     */
    public static function GetHits($pageID, $unique = false){
        global $database;
        self::CreateCountsIfNotPresent($pageID);
        $pageID = $database->escape_value($pageID);
        $unique = $unique ? '1' : '0';
        $q  = "SELECT hitcount FROM hits WHERE ";
        $q .= get_article_id()=$pageID;
        $q .=" AND isunique={$unique}";
        $getHitCount = static::find_by_sql($q);
        if(sizeof($getHitCount) >= 1){
            foreach($getHitCount as $hit){
                return (int)$hit->hitcount;
            }
        }else{
            die("Fatal: Missing hit count from database!");
        }
    }

    /*
     * Returns the total amount of hits to the entire website
     * When $unique is FALSE, it returns the sum of all non-unique hit counts
     * for every page. When $unique is TRUE, it returns the sum of all unique
     * hit counts for every page, so the value that's returned IS NOT the 
     * amount of site-wide unique hits, it is the sum of each page's unique
     * hit count.
     */
    public static function GetTotalHits($unique = false){
        //global $phpcount_con;
        $total = 0;
        $unique = $unique ? '1' : '0';
        $q = "SELECT hitcount FROM hits WHERE isunique={$unique}";
        $count = static::find_by_sql($q);
        foreach($count as $hit){
            $total += (int)$hit->hitcount;
        }
        return $total;
    }

    private static function CountHit($pageID, $unique){
        global $database;
        $unique = $unique ? '1' : '0';
        $safeID = $database->escape_value($pageID);
        $q ="UPDATE hits SET hitcount = hitcount + 1 WHERE ";
        $q .=get_article_id()=$safeID;
        $q .=" AND isunique={$unique}";
        mysqli_query($database->connection,$q);
    }

    private static function CreateCountsIfNotPresent($pageID){
        global $database;
        $pageID = $database->escape_value($pageID);
        $q = "SELECT pageid FROM hits WHERE ";
        $q .=get_article_id()=$pageID;
        $q .=" AND isunique='0'";
        $createCount = static::find_by_sql($q);
        if($q === false || sizeof($createCount) < 1){
            $sql ="INSERT INTO hits(";
            $sql .=get_article_id();
            $sql .=", isunique, hitcount) VALUES(";
            $sql .=$pageID;
            $sql .=", '0', '0')";
            mysqli_query($database->connection,$sql);
        }

        //check unique row
        $q ="SELECT "get_article_id();
        $q .=" FROM hits WHERE ";
        $q .=get_article_id()=$pageID;
        $q .=" AND isunique='1'";
        $createCount = static::find_by_sql($q);
        if($q === false || sizeof($createCount) < 1){
            $sql ="INSERT INTO hits (";
            $sql .=get_article_id();
            $sql .=", isunique, hitcount) VALUES('$pageID', '1', '0')"
            mysqli_query($database->connection,$sql);
            echo mysqli_error($database->connection);
        }
    }
}
4

1 回答 1

5

访问静态类变量需要一个$ 符号,如下所示:

PHPCount::$article_id 

因此,至少这些方法需要改变。

// I'd propose to pass the article ID as a parameter here
function __construct( $theArticleID ){
    PHPCount::$article_id = $theArticleID;         
}
public static function set_article_id($articleid){
   PHPCount::$article_id = $articleid;
}
public static function get_article_id(){
   return PHPCount::$article_id;
}
于 2012-09-01T21:38:13.500 回答