Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Value of Static Property of a Class Using Setters and Getters

Tags:

php

I want to extend this class (which I downloaded) to suit my own needs. When I run call this class I get an error complaining that there is an unexpected = in the constructor.

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);
        }
    }
}
like image 398
arjang27 Avatar asked Sep 01 '12 21:09

arjang27


People also ask

Can we use getter and setter on static variables?

Yes, static setters/getters are allowed as long as both the class variable and the method are made static.

What are static properties of a class?

Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor. However, they can contain a static constructor.

Can we have static properties in C#?

In C#, static means something which cannot be instantiated. You cannot create an object of a static class and cannot access static members using an object. C# classes, variables, methods, properties, operators, events, and constructors can be defined as static using the static modifier keyword.

What are static properties in Javascript?

Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.


1 Answers

Access to static class variables require a $-sign like here:

PHPCount::$article_id 

Thus, at least these methods need to be changed.

// 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;
}
like image 64
SteAp Avatar answered Sep 30 '22 05:09

SteAp