2011-05-10 31 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

我采取了第一种方法!谢谢你的队友! :) –