代码之家  ›  专栏  ›  技术社区  ›  Michael

wpdb无法转换为字符串

  •  0
  • Michael  · 技术社区  · 5 年前

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

    在中转换为字符串 12号线

    <?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' );
    

    安装程序.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);;
        }
    }
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Dmitry Leiko    5 年前

    $this->table_name = $this->wpdb->prefix . 'ved_currencies';
    
        2
  •  1
  •   Mücahit Yılmaz    5 年前

    $this->$wpdb->prefix

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

    $this->wpdb->prefix

    不需要 $ 之后 $this-> 调用类变量。