2011-05-10 30 views
1

我有一個類,它具有檢查並在數據庫中創建表的功能。 爲了做到這一點,我需要使用WordPress $ wpdb對象。在回調期間在類函數中調用外部對象

我需要的功能,只對第一個插件激活運行,所以我使用的功能:

register_activation_hook (__FILE__, array('MemorialCandles', 'dbInstall' )); 

的問題是,我總是得到這個錯誤:

Fatal error: Using $this when not in object context in /home/xxx/xxx/wordpress/wp-content/plugins/MemorialCandles/memorial-candles.class.php on line 77

類代碼:

<?php 

// Global Variables: 
global $wpdb; 
register_activation_hook (__FILE__, array('MemorialCandles', 'dbInstall' )); 

/** 
* Class: MemorialCandles 
* 
* Provides skeleton to the plugin and handles queries and action. 
* 
* @author Dor Zuberi <[email protected]> 
* @copyright 2011 Dor Zuberi 
* @license http://www.php.net/license/3_01.txt 
*/ 
class MemorialCandles 
{ 
    // Variables  
    /** 
    * @var string stores plugin direction - RTL or LTR. 
    */ 
    private $pluginDirection; 

    /** 
    * @var string stores the plugin database table name. 
    */ 
    private $tableName; 

    // Constructor 
    /** 
    * Initiates the plugin, stores and configure the basic setup procedures. 
    * 
    * @return void 
    */ 
    function __construct() 
    { 
     global $wpdb; 

     $this->tableName = $wpdb->prefix . 'memorialcandles'; 
    } 

    // Getters 

    // Setters 

    // Methods 
    /** 
    * Handles the database table creation. 
    * 
    * @return void 
    */ 
    function dbInstall() 
    { 
     global $wpdb; 

     if($wpdb->get_var("SHOW TABLES LIKE `{$this->tableName}`") != $this->tableName) 
     { 
      $sql = "CREATE TABLE `{$this->tableName}` (
         id  int(8) NOT NULL AUTO_INCREMENT, 
         fullName text NOT NULL, 
         message text NOT NULL, 
         postDate text NOT NULL, 
         galleryID int(8) NOT NULL, 

         UNIQUE KEY id(id) 
        );"; 

      require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 
      dbDelta($sql); 
     } 
    } 

    /** 
    * Handles the database table drop procedure. 
    * 
    * @return void 
    */ 
    function dbUninstall() 
    { 
     global $wpdb; 

     $sql = "DROP TABLE IF EXISTS `{$this->tableName}`;"; 

     $wpdb->query($sql); 
    }  
} 

?> 

在此先感謝! :D

+0

該問題的標籤不需要在問題的標題中,以吸引那些熟悉Wordpress的人的興趣,請原諒我編輯刪除該問題。此外,+1在第一次嘗試時管理格式化! :D值得指出的是,網絡上還有一個特定的[Wordpress網站](http://wordpress.stackexchange.com/),可以提供更具體的建議。 –

+0

謝謝David,讚賞! :) 我會在那裏發帖以及我不知道有一個WordPress的特殊論壇:D –

回答

2

要在回調中使用實例方法,回調需要一個實例。你要麼需要創建用於呼叫的實例register_activation_hook

register_activation_hook(__FILE__, array(new MemorialCandles(), 'dbInstall')); 

或使dbInstall一個class方法。

class MemorialCandles { 
    // Variables  
    /** 
    * @var string stores the plugin database table name. 
    */ 
    private static $tableName, $tableSuffix = 'memorialcandles'; 
    ... 

    // Methods 
    /** 
    * Handles the database table creation. 
    * 
    * @return void 
    */ 
    static function dbInstall() { 
     global $wpdb; 
     $tableName = self::$tableName = $wpdb->prefix . self::$tableSuffix; 
     if($wpdb->get_var("SHOW TABLES LIKE `{$tableName}`") != $tableName) 
     { 
      $sql = "CREATE TABLE `{$tableName}` (
         id  int(8) UNSIGNED NOT NULL AUTO_INCREMENT, 
         fullName text NOT NULL, 
         message text NOT NULL, 
         postDate text NOT NULL, 
         galleryID int(8) UNSIGNED NOT NULL, 

         UNIQUE KEY id(id) 
        );"; 

      require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 
      dbDelta($sql); 
     } 
    } 
    ... 
} 
+0

我採取了第一種方法!謝謝你的隊友! :) –