2014-10-04 19 views
-1

我有一個類通知。

但是,這根本不起作用。 公共函數deleteNotification。將無法在96線上工作。 我已經擁有。公共功能,公共靜態功能和靜態功能。 但他們都沒有工作。

誰能看到我的幫助。謝謝! 對不起我的英文不好

<?php 


class Notification { 

    /** 
    * Config 
    * 
    * @access private 
    */ 
    private static $config; 

    /** 
    * Database 
    * 
    * @access private 
    */  
    private $db; 

    /** 
    * Session 
    * 
    * @access private 
    */  
    private $session; 

    /** 
    * Constructor 
    * 
    * @access public 
    */ 
    public function __construct() { 

     self::$config = config_load('notification'); 

     $this->db = new Database(); 
     $this->session = new Session(); 


    } 


    var $type; 
    var $to_user; 
    var $from_user; 
    var $reference; 
    var $timestamp; 
    var $newcount; 

    /** 
    * getAllNotifications 
    * 
    * @access public 
    */ 
    public function getAllNotifications() { 
    $this->newcount = Notification::newCount($this->to_user); 
     foreach ($this->db->query("SELECT * FROM " . 
      self::$config['table_notification'] . 
      " WHERE to_user = 1 ORDER BY `timestamp` DESC LIMIT 10") as $row) { 

      $result[] = array(
       'id'   =>$row['id'], 
       'to_user' =>$row['to_user'], 
       'from_user' =>$row['from_user'], 
       'reference' =>$row['reference'], 
       'type'  =>$row['type'], 
       'seen'  =>$row['seen'], 
       'timestamp' =>$row['timestamp'] 
      ); 

     } 
    if ($result) { 
     return $result; 
    } 
    return false; //none found 
    } 

    /** 
    * Get newCount 
    * 
    * @access public 
    */ 
    public function newCount($user) { 
    $row = ceil($this->db->row_count("SELECT * FROM " . 
      self::$config['table_notification'] . " WHERE to_user = 1 AND seen = 0 ")); 
    return $row; 
    } 

    /** 
    * deleteNotification 
    * 
    * @access public 
    */ 
    public static function deleteNotification($id) {  
    $where = array(
     'id' => $id 
    ); 

    $this->db->where($where); 
    $this->db->delete(self::$config['table_notification']); 
    } 

} 
?> 

$id在這個文件中定義:

update_notifications.php:

<?php  
include("common.php"); 
$id = 1; 
$action = $_REQUEST['action']; 
switch($action) { 
    case "seen": 
if (isset($_REQUEST['notifications'])) { 
    $notifications = json_decode($_REQUEST['notifications']); 
    foreach ($notifications as $notification) { 
    if (is_numeric($notification)) Notification::Seen($notification); 
    } 
} 
break; 
case "delete": 
$notification = $_REQUEST['notification']; 
if (is_numeric($notification)) Notification::deleteNotification($notification); 
break; 
} 
?> 

在這個文件我已經設置在第3行$id = 1;,看看它是否工作。 worldofjr的選項也不起作用。這給出了以下erorr到:

Catchable fatal error: Argument 2 passed to Notification::deleteNotification() must be an instance of Notification, none given, called in C:\Users\Anthony\Desktop\server\htdocs\update_notifications.php on line 17 and defined in C:\Users\Anthony\Desktop\server\htdocs\libraries\Notification.php on line 103

+0

你如何實例化類並調用函數? – jdog 2014-10-04 02:57:01

+0

在調用函數後是否有任何錯誤? – 2014-10-04 02:58:06

回答

4

靜態方法不屬於一個對象的實例,$this涉及到的類的實例。所以,在靜態方法deleteNotification中沒有$this

你真的需要這種方法是靜態的嗎?

1

A static方法不使用一個對象的實例,它是針對使用Class的相關函數。

可以刪除方法的static關鍵字,也可以使用參數將類的實例傳遞給方法。

public static function deleteNotification($id,Notification $n) {  
    $where = array(
     'id' => $id 
    ); 

    $n->db->where($where); 
    $n->db->delete(self::$config['table_notification']); // ?? 
} 

要調用此方法static您需要提供一個ID($id)和要從刪除Notification一個實例。

deleteNotification($id,$notification); 

此外,你需要以刪除一個值(在那裏我已經評論??)編寫訪問(和/或刪除)方法$config

希望這會有所幫助。

相關問題