WordPress PHP Extended Classes Refer to 'Wrong' Static Variables -
i have wordpress plugin 2 extended classes, area , loc, each of has helper function get. each class has static variables table_name. if call loc::get($id) directly, works expected. if, however, call loc::get($id) area, uses table_name area rather location.
can explain how correct this? regards,
class _base { function get($id) { $instance = new self(); $sql = "select * " . static::$table_name . " id=$id"; return $sql; } } class area extends _base { static $table_name = "area"; function getloc($id) { $sql = loc::get($id); return $sql; } } class loc extends _base { static $table_name = "loc"; }
$sql = area::get(1); // -> "select * **area** id=1" $sql = loc::get(1); // -> "select * **loc** id=1" $sql = $area->getloc(1); // -> "select * **area** id=1"
i think problem run non static method get
static. when changed start work fine. try code below:
<?php class _base { static function get($id) { $instance = new self(); $sql = "select * " . static::$table_name . " id=$id"; return $sql; } } class area extends _base { static $table_name = "area"; function getloc($id) { return loc::get($id); } } class loc extends _base { static $table_name = "loc"; } var_dump(area::get(1)); var_dump(loc::get(1)); var_dump((new area)->getloc(1));
Comments
Post a Comment