Php 摘要记录

Php 摘要记录,php,constructor,abstract,Php,Constructor,Abstract,我试图创建一种自动机制,使用抽象类将数据从数据库加载到对象中,该抽象类将在必要时进行扩展 这是我的简化想法,目前不起作用,因此如果有人能解释我为什么做错了,以及我做错了什么: 抽象类: abstract class abstractRecord{ public $data = array(); public function __constructor($id = null){ global $db; // eg. PDO $this->

我试图创建一种自动机制,使用抽象类将数据从数据库加载到对象中,该抽象类将在必要时进行扩展

这是我的简化想法,目前不起作用,因此如果有人能解释我为什么做错了,以及我做错了什么:

抽象类:

abstract class abstractRecord{

    public $data = array();

    public function __constructor($id = null){
        global $db; // eg. PDO
        $this->db = $db;


        if(!is_null($id) && intval($id) > 0)
            $this->fillData($id);

    }

    public function fillData($id){

        $this->data = $db->getAll("SELECT * FROM ".self::$table." WHERE ".self::$id."='$id'");

    }

}
现在,一些类将扩展abstratRecord,并且在初始化时应该使用抽象构造函数来填充数据(如果提供了有效的id)

class User extends abstractRecord{

    public static $id = "`id`";
    public static $table = "`users`";

    // some other functions for controlling user...


}
在最后的用法

$user = new User(15);  // so user data from db with id=15 should be loaded into object data property by abstractRecord class
所以这只是一个简单的版本,应该有更多的验证,getters setters接口


现在,我只想知道这里出了什么问题以及原因,并且是否可以在不调用父对象的情况下使用此流::u构造函数(),据我所知,在这种情况下类应该扩展抽象的u构造函数,或者可能我错了?

您的抽象类的构造函数方法拼写错误。需要是
\uu construct()
。然后需要在扩展类中调用该构造函数,如下所示:

class User extends abstractRecord{
    public function __construct() {
        // call to abstractRecore construct
        parent::__construct();
    }
}

好的,我设法解决了这个问题并建立了工作机制,所以看起来是这样的:

抽象类抽象记录{

protected $data = array();
public static $table;
public static $pK;

public function __construct($id = null, $table = false, $pK = false){

if($table == false || $pK == false)
    return false;

self::$table = $table;
self::$pK = $pK;

if (!is_null($id) && intval($id) > 0)
        $this->fillData($id);

}

protected function fillData($id){
    global $db; // db solution

    $this->data = $db->("SELECT * FROM ".self::$table." WHERE ".self::$pK."='$id' LIMIT 1");

}

// some other useful functions
public static $table =  " `users` ";
public static $pK = " `userID` ";

public function __construct($id = null){

    parent::__construct($id, self::$table, self::$pK);
}

// some other functions ...
}

某类

类用户扩展抽象记录{

protected $data = array();
public static $table;
public static $pK;

public function __construct($id = null, $table = false, $pK = false){

if($table == false || $pK == false)
    return false;

self::$table = $table;
self::$pK = $pK;

if (!is_null($id) && intval($id) > 0)
        $this->fillData($id);

}

protected function fillData($id){
    global $db; // db solution

    $this->data = $db->("SELECT * FROM ".self::$table." WHERE ".self::$pK."='$id' LIMIT 1");

}

// some other useful functions
public static $table =  " `users` ";
public static $pK = " `userID` ";

public function __construct($id = null){

    parent::__construct($id, self::$table, self::$pK);
}

// some other functions ...
}

抱歉,如果有什么拼写错误


谢谢你的帮助

啊,是的,这解决了一些问题,现在我在通过抽象类访问子类中定义的属性时遇到了问题,这对功能性非常关键,所以如果我定义公共静态$table=“table\u name”,我需要能够在抽象类中使用此属性。。。