2011-05-10 28 views
1

私は、データベースにテーブルをチェックして作成するクラスを持っています。 これを行うには、私はWordPress $ wpdbオブジェクトを使用する必要があります。コールバック中にクラス関数の外部オブジェクトを呼び出す

は、私は最初のプラグインの活性化に実行する機能を必要とするので、私は、関数を使用します。

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

クラスコード:

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

問題は、私は常にこのエラーが出るということですが:

<?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ありがとう、それを感謝します! :) 私はそこにも投稿します。私はワードプレスのための特別フォーラムがあるのを知らなかった:D –

答えて

2

インスタンスメソッドをコールバックで使用するには、コールバックにインスタンスが必要です。

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

またはdbInstallclassメソッドを作る:あなたはregister_activation_hookへの呼び出しのためのインスタンスを作成する必要がありますどちらか。

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

私は最初の方法を取った!あなたが仲間に感謝! :) –

関連する問題