回调期间在类函数中调用外部对象

发布于 2024-11-06 10:59:40 字数 2532 浏览 4 评论 0原文

我有一个类,它具有检查并在数据库中创建表的函数。 为此,我需要使用 WordPress $wpdb 对象。

我需要该函数仅在第一次插件激活时运行,所以我使用该函数:

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

问题是我总是收到此错误:

致命错误:在第 77 行 /home/xxx/xxx/wordpress/wp-content/plugins/MemorialCandles/memorial-candles.class.php 中不在对象上下文中时使用 $this

类代码:

<?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

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 <[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 );
    }    
}

?>

Thanks in advance! :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

櫻之舞 2024-11-13 10:59:40

要在回调中使用实例方法,回调需要一个实例。您需要为调用 register_activation_hook 创建一个实例:

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

或者将 dbInstall 设为 方法。

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 );
        }
    }
    ...
}

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 );
        }
    }
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文