doumo6356 2012-11-14 17:15
浏览 92

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

I'm have a DatabaseObject Class that has select, insert, update and delete methods defined. The purpose of the class is to represent a subset of rows in my MySQL database as a PHP object. The constructor calls MySQL's SELECT command and the destructor calls the DELETE command (unless I ask it not to).

Here is my DatabaseObject class:

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. For example, my User class is for my user table. 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. 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?

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.

  • 写回答

1条回答 默认 最新

  • doumi1311 2012-11-14 17:24
    关注
    <?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

    Updated: Using self instead of $this-> example.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧