简体   繁体   English

父类的静态var无效

[英]Static var from parent class not working

I'am building a wp plugin and in the parent class called database I want to create some settings and such. 我正在构建一个wp插件,在名为database的父类中,我想创建一些设置等。 One of these settings are the naming of the db tables, and to do things right I do use the wpdb prefix(allows users to changes the table prefix). 其中一个设置是db表的命名,为了做正确的事我使用wpdb前缀(允许用户更改表前缀)。

Because I just got started with OOP I dont get this working ;-) 因为我刚刚开始使用OOP我不会让这个工作;-)

the example(basic idea) 例子(基本想法)

  class database{

      public static $dbTableName = 'mynewtable';
      public static $dbTable;

      public function __constructor(){
          global $wpdb;

          $this->dbTable = $wpdb->prefix . $dbTableName;    
      }

  }  

  class install extends database{

      public static function getTable(){
          parent::$dbTable;// not working

          // output should be wp_mynewtable

          //do some stuff here with the variable
      }
  }

The problem is, you're assigning a value to your static variable in a non-static way (turn up your error reporting to E_ALL ). 问题是,您是以非静态方式为静态变量赋值(将错误报告发送到E_ALL )。 You're also relying on the constructor to set values which means that a class must be created. 您还依赖于构造函数来设置值,这意味着必须创建一个类。 This rules out the static function from being able to access said variable. 这排除了静态函数能够访问所述变量。

You should also do away with that global nonsense. 你也应该废除这种全球性的废话。 Something like this... 像这样......

namespace something\that\does\not\clash\with\wordpress;

use \wpdb;

class database {
    protected static $dbTableName = 'mynewtable';
}

class install extends database {
    public static function getTable(wpdb $wpdb) {
        return $wpdb->prefix . parent::$dbTableName;
    }
}

From a class you can make an instance, for example the class Person can represent multiple persons, but if you use a static variable that will be the same over all instances made from that class. 从类中可以创建一个实例,例如,Person类可以表示多个人,但是如果使用的静态变量与从该类生成的所有实例相同。 The static variable tablePrefix can be 'wp_'. 静态变量tablePrefix可以是'wp_'。 Thus: 从而:

class DbTable
{
    protected static $preFix = 'wp_';
}

Where every you use self::$preFix you have the same value, if you have a public $tablename; 如果你有一个public $tablename; ,那么每个你使用self :: $ preFix你都有相同的值public $tablename; you can change this for every instance. 您可以为每个实例更改此设置。

class DbTable
{
    protected static $preFix = 'wp_';

    public $name;

    // first function called when making a instance
    public function __construct($name)
    {
        $this->name = $name;
    }
}

// first instance for table person
$nameTable = new DbTable('person');
// second instance for table car
$carTable = new DbTable('car');

if you dump values of the objects with the function print_r you can see that the variable 'name' has different values. 如果使用函数print_r转储对象的值,则可以看到变量“name”具有不同的值。 If we now make use of the static variable preFix you can see this will stay the same overall instance of the class DbTable. 如果我们现在使用静态变量preFix,你会发现它将保持类DbTable的整个实例。 We add the following function to the class. 我们将以下函数添加到类中。

public function GetFullTableName()
{
    return self::$preFix . $this->name;
}

If we now display the tablenames for the two instance you will get the following result: 如果我们现在显示两个实例的表名,您将得到以下结果:

echo $nameTable->GetFullTableName(); // output: wp_person
echo $carTable->GetFullTableName(); // output: wp_car

You can add static function to the class to change the static variable, you can also make the variable public to access it directly. 您可以向类添加静态函数以更改静态变量,也可以将变量public设置为直接访问它。 The advantage is that the variable will be the same over all instance of the class, thus a change will affect all instances. 优点是变量在类的所有实例上都是相同的,因此更改将影响所有实例。 I advice you to read more about when to create instances and when to use static methods or variables. 我建议您阅读有关何时创建实例以及何时使用静态方法或变量的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM