English 中文(简体)
PHP 最近的静态绑定错误
原标题:PHP Late Static Binding error

我正在尝试学习如何使用 LSB 。 我正在尝试将我常用的 db 方法分解为 < code> 类数据库Object , 并将其推广到所有使用这些方法的类别。 普通 db 方法为 < code> find_ by_ id () , < code > create () , < code> delete () , < code > < () etete () 等。

$username = trim($_POST[ username ]);
$password = trim($_POST[ password ]);

$found_personnel = Personnel::authenticate($username, $password);

Classes

class DatabaseObject {


  public static function find_by_sql($sql="") {
    global $database;
    $result_set = $database->query($sql);
    $object_array = array();
    while ($row = $database->fetch_array($result_set)) {
        $object_array[] = self::instantiate($row);
    }

    return $object_array;
  }

  private static function instantiate($record) {
    $object = new self;

    foreach($record as $attribute=>$value){
        if ($object->has_attribute($attribute)) {
            $object->$attribute = $value;
        }
    }
    return $object;
  }

  private function has_attribute($attribute) {
    $object_vars = $this->attributes();

    return array_key_exists($attribute, $object_vars);
  }

  protected function attributes() {
    $attributes = array();

    //this static::$db_fields is where my problem is
    foreach(static::$db_fields as $field) {

      if(property_exists($this, $field)) {
        $attributes[$field] = $this->$field;
      }
    }
    return $attributes;
  }
}

class Personnel extends DatabaseObject {
  protected static $table_name = "personnel";

  //this is the property I am trying to access in DatabaseObject
  protected static $db_fields = array( id ,  username ,  password ,  first_name ,  last_name ,  email ,  permissions );

public static function authenticate($username="", $password="") {
    global $database;
    $username = $database->escape_value($username);
    $password = $database->escape_value($password);

    $sql = "SELECT * FROM " . self::$table_name . " ";
    $sql .= "WHERE username =  {$username}  ";
    $sql .= "AND password =  {$password}  ";
    $sql .= "LIMIT 1";

    $result_array = parent::find_by_sql($sql);
    return !empty($result_array) ? array_shift($result_array) : false;

}
}

The error I get:

Fatal error: Access to undeclared static property: DatabaseObject::$db_fields
最佳回答

这个错误听起来像有一个 DatabaseObject 实例,而不是一个 Prime 实例。

问题回答

暂无回答




相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签