【问题标题】:wpdb could not be converted to stringwpdb 无法转换为字符串
【发布时间】:2019-11-25 12:38:11
【问题描述】:

你能帮我解决这个问题吗(代码中标记了第 12 行):

( ! ) 可捕获的致命错误:wpdb 类的对象不能是 转换为字符串 C:\OSPanel\domains\ved\wp-content\plugins\ved-currencies\installer.php 在第 12 行

ved-currencies.php

<?php
function installer(){
    include('installer.php');
    $installer = new Installer;
    $installer -> activate();
}

register_activation_hook( __file__, 'installer' );

function deactivate(){
    include('installer.php');
    $installer = new Installer;
    $installer -> deactivate();    ;
}

register_deactivation_hook( __FILE__, 'deactivate' );

installer.php

<?php

class Installer {

    private $wpdb;
    private $table_name;
    private $charset_collate;

    public function __construct() {
        global $wpdb;
        $this->wpdb = $wpdb;
        $this->table_name = $this->$wpdb->prefix . 'ved_currencies'; // Line 12
        $this->charset_collate = $wpdb->get_charset_collate();
    }

    public function activate(){
        $sql = "CREATE TABLE $this->table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT UNIQUE,    
        date date not null,
        char_code varchar(3) NOT NULL,
        name varchar(40) NOT NULL,
        nominal int(9) NOT NULL,
        value DECIMAL(20,20) NOT NULL,
        PRIMARY KEY  (date, char_code)
        )    $this->charset_collate;";
        if ( ! function_exists( 'maybe_create_table' ) ) {
            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        }
        maybe_create_table( $this->table_name, $sql);
    }

    public function deactivate(){
        $sql = "drop table if exists $this->table_name";
        dbDelta($sql);;
    }
}

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    $this-&gt;$wpdb-&gt;prefix

    不是对象,因为正确的调用方式应该是

    $this-&gt;wpdb-&gt;prefix

    $this-&gt; 之后不需要$ 来调用类变量。

    【讨论】:

      【解决方案2】:

      希望帮助:

      $this->table_name = $this->wpdb->prefix . 'ved_currencies';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-19
        • 1970-01-01
        • 1970-01-01
        • 2021-11-15
        • 2018-02-19
        • 2012-01-07
        • 2018-06-15
        相关资源
        最近更新 更多