ページに広告を表示するクラスがあります。どの広告が表示されたかを追跡したいので、数値の配列を保持するプライベートな静的メンバーをクラスに追加しました。次のクエリからそれらの ID を除外する方法として、db クエリの結果から静的メンバーに ID を追加したいと考えています。これにより、表示された広告が同じページに再度表示されるのを防ぐことができます。
class ADS {
private static $excluded_ads = array();
function get_ads() {
// run db query and assign $ads to the resulting array
$ads = $this->query();
// Iterate through result and add the IDs of each row to the static array
foreach ($ads as $ad) {
self::$excluded_ads[] = $ad->ID;
}
}
function query() {
// Use local variable to hold string of excluded ads
$excluded_ads = $this->sql_get_excluded_ads();
// run the db query and use the class static member to exclude results
// SELECT * FROM ....
// WHERE ...
// AND p.ID NOT IN ($excluded_ads)
}
function sql_get_excluded_ads() {
if (empty(self::$excluded_ads)){
return '-1';
} else {
return implode(',',self::$excluded_ads);
}
}
}
$ads_class = new ADS();
$ads_class->get_ads();
ページをロードするTrying to get property of non-object
と、行に対してこのエラーが発生しますself::$excluded_ads[] = $ad->ID;
静的クラス メンバーは PHP でこのように機能しますか? この値は、ページが読み込まれるたびにリセットされることはわかっていますが、それが私が望む機能です。これには、現在のページ/プロセスの値のみを含めてからリセットしたいと考えています。