2011-05-10 5 views
1

데이터베이스에 테이블을 확인하고 만드는 클래스가 있습니다. 그래서 나는 워드 프레스 $ 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

질문에 대한 태그 워드 프레스에 익숙한 사람들의 관심을 끌기 위해 질문의 제목에있을 필요가없는, 내가 그것을 제거하기 위해 편집 용서해주십시오. 또한, 첫 번째 시도에서 서식을 관리하기위한 +1! : D 또한 네트워크에 특정 정보를 제공 할 수있는 특정 [Wordpress 사이트] (http://wordpress.stackexchange.com/)가 있음을 지적 할 가치가 있습니다. –

+0

데이비드 감사합니다. :) 내가 거기에 게시 할뿐만 아니라 나는 워드 프레스에 대한 특별한 포럼을 몰랐다 : D –

답변

2

콜백에서 인스턴스 메소드를 사용하려면 콜백에 인스턴스가 필요합니다. 당신은 register_activation_hook에게 호출 인스턴스를 생성해야합니다 중 하나

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

또는 dbInstallclass 방법을 확인하십시오.

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

나는 첫 번째 방법을 택했다! 네가 고마워! :) –

관련 문제