简体   繁体   中英

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. 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).

Because I just got started with OOP I dont get this working ;-)

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 ). 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. The static variable tablePrefix can be '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; 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. If we now make use of the static variable preFix you can see this will stay the same overall instance of the class 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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