Php 在回调期间调用类函数中的外部对象
我有一个类,它有一个在数据库中检查和创建表的函数。 为此,我需要使用WordPress$wpdb对象 我需要只在第一次激活插件时运行该函数,因此我使用该函数:Php 在回调期间调用类函数中的外部对象,php,class,global,wordpress,Php,Class,Global,Wordpress,我有一个类,它有一个在数据库中检查和创建表的函数。 为此,我需要使用WordPress$wpdb对象 我需要只在第一次激活插件时运行该函数,因此我使用该函数: register_activation_hook ( __FILE__, array( 'MemorialCandles', 'dbInstall' ) ); 问题是,我总是遇到以下错误: 致命错误:在第77行的/home/xxx/xxx/wordpress/wp content/plugins/memorialcands/mem
register_activation_hook ( __FILE__, array( 'MemorialCandles', 'dbInstall' ) );
问题是,我总是遇到以下错误:
致命错误:在第77行的/home/xxx/xxx/wordpress/wp content/plugins/memorialcands/memorial-cands.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 <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 );
}
}
?>
提前谢谢!:要在回调中使用实例方法,回调需要一个实例。您需要为调用
register\u activation\u hook
创建一个实例:
register_activation_hook(__FILE__, array(new MemorialCandles(), 'dbInstall'));
或者将dbInstall
作为一种方法
问题的标签不需要出现在问题的标题中,以吸引熟悉Wordpress的人的兴趣,请原谅我编辑以删除该标签。另外,+1用于在第一次尝试时管理格式!:D另外,值得指出的是,网络上有一个特定的网站,可以提供更具体的建议。谢谢你,大卫,非常感谢!)我也会在那里发布我不知道有一个专门为WordPress:D的论坛
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 );
}
}
...
}