简体   繁体   English

如何在PHP类中声明静态变量,但在扩展基类的类中定义它?

[英]How do I declare a static variable in a PHP class but define it in a class which extends the base class?

I'm have a DatabaseObject Class that has select, insert, update and delete methods defined. 我有一个DatabaseObject类,它定义了select,insert,update和delete方法。 The purpose of the class is to represent a subset of rows in my MySQL database as a PHP object. 该类的目的是将MySQL数据库中的行子集表示为PHP对象。 The constructor calls MySQL's SELECT command and the destructor calls the DELETE command (unless I ask it not to). 构造函数调用MySQL的SELECT命令,析构函数调用DELETE命令(除非我不这样做)。

Here is my DatabaseObject class: 这是我的DatabaseObject类:

class DatabaseObject {  
    protected $table;
    protected $index_field;

    private $data;

    function __construct($id) {
        //MySQL SELECT and assign resource to data
    }

    function __destruct() {
        //MySQL UPDATE
    }

    public function insert($data) {
        global $db;     //Database Class for using MySQL

        $fields = array();
        $values = array();

        foreach ($data as $field => $value) {
            $fields[] = "`".$field."`";
            $values[] = "'".$db->escape($value)."'";
        }

        $sql = "INSERT INTO ".$this->table." (".join(', ', $fields).") VALUES (".join(', ', $values).")";
        if($db->query($sql)) {
            return $db->insertID();
        } else {
            return false;
        }
    }

    //More methods; update(), delete(), select()
}

I extend the DatabaseObject class when I want to access a specific table. 当我想访问特定的表时,我扩展了DatabaseObject类。 For example, my User class is for my user table. 例如,我的User类用于我的用户表。 Here it is: 这里是:

class User extends Object {
    public static $anonymous_data = array('user_id' => 0, 'user_level' => ANONYMOUS, 'username' => 'Anonymous');

    function __construct($user_id = NULL) {
        global $db;
        $this->db = $db;

        $this->table = USER_TABLE;
        $this->index_field = 'user_id';

        parent::__construct($user_id);
    }

    function __destruct() {
        parent::__destruct();
    }

    //Other user methods; login(), logout() etc.
}

Now, I would like to be able to call the insert method from the user class without having already instantiated the User class. 现在,我希望能够从用户类调用insert方法,而无需实例化User类。 I'm pretty sure, I have to make it static to do so. 我很确定,我必须让它静止。 How do I make the insert method static, but still allow it to use the table which was defined in the extension class? 如何使insert方法保持静态,但仍然允许它使用扩展类中定义的表?

In other words, I want to be able to do this: 换句话说,我希望能够做到这一点:

User::insert($data);        //INSERT into user table
AnotherClass::insert($data);    //INSERT into another table

...without instantiating either class. ...没有实例化任何一个类。

<?php
class Foo {
    public static $anonymous_data = array();

    public static function aStaticMethod() {
        self::$anonymous_data = array(key, value);

        foreach (self::$anonymous_data as $key => $value) {
             echo "$key => $value";
        }
    }
}

Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
?>

For a static method. 对于静态方法。 However be aware static variables CAN NOT be accessed this way. 但请注意,静态变量不能以这种方式访问​​。 Read more about this example and the static key word here: PHP.net 在这里阅读有关此示例和静态关键字的更多信息: PHP.net

Updated: Using self instead of $this-> example. 更新:使用self而不是$this->示例。

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

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