开发者

Call External Object in Class Function During Callback

I've a class which has a function that check and creates table in database. In order to do so i need to use WordPress $wpdb object.

I need the function to run only on first plugin activation, so i use the function:

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

The problem is that i get always this error:

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

the class code:

<?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 <dor@zubri.me>
 * @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 );
    }    
}

?>

Thanks in advance! :D


To use an instance method in a callback, the callback needs an instance. You'll either need to create an instance for the call to register_activation_hook:

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

or make dbInstall a class method.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜