php – 使用Setter和getters更改类的静态属性的值
发布时间:2020-12-13 16:47:00 所属栏目:PHP教程 来源:网络整理
导读:我想扩展这个类(我下载的)以满足我自己的需要.当我运行调用这个类时,我得到一个错误,抱怨构造函数中有一个意外的=. define("HIT_OLD_AFTER_SECONDS",4 * 7 * 24 * 3600);class PHPCount extends DatabaSEObject { protected static $table_name = "hits"; pr
我想扩展这个类(我下载的)以满足我自己的需要.当我运行调用这个类时,我得到一个错误,抱怨构造函数中有一个意外的=.
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 .=",hitcount) VALUES('$pageID','1','0')" mysqli_query($database->connection,$sql); echo mysqli_error($database->connection); } } } 解决方法
访问静态类变量需要$-sign,如下所示:
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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |