【问题标题】:Fatal Error using $this when not in object context不在对象上下文中使用 $this 的致命错误
【发布时间】:2015-09-16 09:34:19
【问题描述】:

我正在尝试开发一个插件。该插件已经开发,但是开发者没有给我们任何支持。当我尝试激活时,出现以下错误并且它没有被激活。

致命错误:在 C:\xampp\htdocs..\wp-content\plugins\sjr-product-import\functions.php 第 238 行的对象上下文中使用 $this

行号和相关函数如下所示。 我不知道可能是什么问题。

/**
* Installation. Runs on activation.
* @since   1.0.0
* @return  void
*/
function install(){

global $wpdb;

$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE {$this->db_table} (
          `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
          `row_id` varchar(22) DEFAULT NULL,
          `title` varchar(255) DEFAULT '',
          `description` text,
          `google_product_category` text,
          `product_type` text,
          `link` text,
          `image_link` text,
          `additional_image_link` text,
          `condition` text,
          `availability` text,
          `price` text,
          `sale_price` text,
          `sale_price_effective_date` text,
          `brand` text,
          `gtin` text,
          `mpn` varchar(22) DEFAULT NULL,
          `item_group_id` int(22) DEFAULT NULL,
          `color` text,
          `material` text,
          `pattern` text,
          `size` text,
          `gender` text,
          `age_group` text,
          `adwords_labels` text,
          `custom_label_0` text,
          `custom_label_1` text,
          `custom_label_2` text,
          `custom_label_3` text,
          `custom_label_4` text,
          `feed_file` varchar(255) DEFAULT NULL,
          `modified` datetime DEFAULT NULL,
          PRIMARY KEY (`id`),
          UNIQUE KEY `row_id` (`row_id`),
          KEY `title` (`title`),
          KEY `item_group_id` (`item_group_id`)
        ) $charset_collate;";

require_once ABSPATH . 'wp-admin/includes/upgrade.php';

dbDelta( $sql );
} // End install ()

请帮忙。谢谢。

【问题讨论】:

    标签: php wordpress fatal-error


    【解决方案1】:

    你有一个错误:

    $sql = "CREATE TABLE {$this->db_table} (
    

    这里,$this 未定义。

    把它当作一个全局变量。

    为此,请将global $wpdb; 更改为global $wpdb, $this;

    或者,检查您应该从哪里获得 db_table 的值,用适当的变量/对象替换 $this->db_table

    【讨论】:

      【解决方案2】:
      if (!is_object($wpdb)) {
          require_once ABSPATH . 'wp-admin/includes/FILE NAME CLASS  OF YOUR FUNCTION get_charset_collate.php'; 
          $wpdb = new 'CLASS_NAME OF YOUR FUNCTION get_charset_collate';
      }
      

      并尝试将 {$this->db_table} 替换为 {$wpdb->db_table} 注意:这只是检查对象变量的想法。

      【讨论】:

      • 对不起,我没有得到这个
      【解决方案3】:

      你应该在functions.php中有类似下面的代码来使用$this

      class SomeClass {
          var $db_table = 'my_table';
          ...
          function install() {
              global $wpdb;
      
              $charset_collate = $wpdb->get_charset_collate();
      
              $sql = "CREATE TABLE {$this->db_table} (
      

      但您可能没有类似的代码来从var $db_table 获取$this->db_table

      所以将第 238 行替换为

      $sql = "CREATE TABLE my_table (
      

      测试它是否有效

      【讨论】:

        猜你喜欢
        • 2010-12-11
        • 2011-01-21
        • 2017-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多