doutongwei4380 2015-08-09 10:13
浏览 28
已采纳

访问静态定义表属性

I'm really bad at OOP and I can't work out this inherited code I've been given.

This is part of the generic Model class;

abstract class Model
{  
  protected static $_tableName  = false;
  protected static $_itemName   = false;

  public static function tableName()
  {
    return static::$_tableName;
  }

  public static function itemName()
  {
    return static::$_itemName;
  }

How do I set the tablename in the Class that I have created???;

class Payments extends Model {

  //public $_tableName;  

  public function __construct()
  {
    $this->$_tableName  = 'payments'; //line 13
  }

}

I get an error Undefined variable: _tableName in /var/www/html/lib/Local/Models/Payments.php on line 13 when I don't set it as a parameter. and an error Cannot redeclare static XXX\Model::$_tableName when I do.

UPDATE

When I try to use the find method with this abstract Model, it's not setting the tableName;

public static function find($idOrWhere = false, $params = array(), $limit = false)
{
    $sql    = "SELECT * FROM " . static::tableName();

I don't know how to set that now. It just ignores what I have put in my class.

  • 写回答

2条回答 默认 最新

  • dongqi19827 2015-08-09 10:16
    关注

    You have to remove the $ when accessing a class property:

    class Payments extends Model 
    {
        public function __construct() 
        {
            $this->_tableName  = 'payments';
        }
    }
    

    Indeed this is irritating, but that's the way php syntax works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗