在面向对象中设置全局变量和数据库php

Set up global variables and database in an object oriented php

这是我的问题:

if ( ! defined( 'WPINC' ) ) die;
if ( ! defined( 'ABSPATH' ) ) exit;

if ( !class_exists( 'MyPlugin' ) ) {

    class MyPlugin {

        protected $MyPluginTable;
        protected $wpdb;
        protected $MyPluginPerfix = 'MyPlugin_';

        public function __construct() {

            define( $MyPluginPerfix' . _ROOTDIR', plugin_dir_path(__FILE__) );

            register_activation_hook(__FILE__, array($this ,'MyPluginInstall') );
            register_deactivation_hook( __FILE__, array($this ,'MyPluginDeactivation') );

            global $wpdb;
            $this->wpdb = $wpdb;
            $this->MyPluginTable = $this->wpdb->prefix . "MyPluginTable";

        }
}

如您所见,我正在尝试设置一些常量,以便我可以在我的代码(一个 WordPress 插件)中的任何地方重复使用。

第一个问题是定义的常量不起作用。

第二个问题是在PHP中定义全局变量的方式,我对调用数据库的方式很好:

$this->NetrackTable

但是我在数据库中定义 table 的方式(作为全局)太冗长了。

我想知道是否有更好的方法来使用和定义 PHP 中的那些实体,以便编写更优雅的代码。

提前致谢

您可以在 Class 的 __construct 方法中设置变量。