php - 如何使 CodeIgniter 实例成为 PHP 类的属性?

标签 php mysql codeigniter

我在 PHP 中创建了一个用户数据库抽象类,它扩展了我创建的称为 DBO(数据库对象)的基类。 DBO 对象的工作只是将 $db 作为我的 codeigniter 的 $db 引用。

理想情况下,在我的 User 对象中,我可以执行 $this->db->insert() 并访问我的 codeigniter 的数据库对象。

这是我的代码:

DBO:

class DBO {

    public $db;
    public $ci;

    //put your code here
    public function __construct() {
        $ci =& get_instance();
        $this->db =& $ci->db;
    }
}

USER:(扩展 DBO)

class User extends DBO{
    /** User id of the user
     * @var int  */
    public $user_id;
    /** User's first name in english
     * @var string  */
    public $first_name;
    /** User's last name in english
     * @var string  */
    public $last_name;
    /** User's name in Korean 
     * @var string */
    public $korean_name;
    /** User's phone number
     * @var string  */
    public $phone;
    /** User's email address
     * @var string  */
    public $email;

    /**
     * Creates a new user from a row or blank.
     * Creates a new user from a database row if given or an empty User object if null
     * 
     * @param Object $row   A row object from table: admin_users
     */
    public function __construct(stdClass $row = null){
        if($row){
            if(isset($row->user_id)) $this->user_id = $row->user_id;
            if(isset($row->first_name)) $this->first_name = $row->first_name;
            if(isset($row->last_name)) $this->last_name = $row->last_name;
            if(isset($row->korean_name)) $this->korean_name = $row->korean_name;
            if(isset($row->phone)) $this->phone = $row->phone;
            if(isset($row->email)) $this->email = $row->email;
        }
    }
    /**
     * Saves this user to the database.
     * @return boolean  Whether this was successfully saved to the database.
     */
    public function create(){
        $data = array(
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'korean_name' => $this->korean_name,
            'phone' => $this->phone,
            'email' => $this->email,
        );
        if($this->db->insert(Tables::$USERS, $data) && $this->db->affected_rows() == 1){
            $this->user_id = $this->db->insert_id();
            return true;
        } else {
            return false;
        }
    }

但是每当我尝试使用$this->db->insert()时,它都会说Call to a member function insert() on a non-object.

有办法让这个工作成功吗?或者它根本上是错误的吗?我的 User 类是否应该仅保存信息,并将 User 对象传递给我的 Users 模型对象并让它运行数据库功能?

感谢您的帮助

最佳答案

您缺少对父构造函数的调用。使用

parent::__contstruct()

在你的用户类构造函数中。通过这样做,将调用父类构造函数,因此数据库对象将被初始化,您将能够使用它。

您还应该阅读此内容

http://www.techflirt.com/tutorials/oop-in-php/inheritance-in-php.html

理解继承。请阅读有关 OOP 的内容,以便您了解 PHP 中 OOP 提供的功能。

关于php - 如何使 CodeIgniter 实例成为 PHP 类的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20757419/

相关文章:

php - 未记录Codeigniter SQLite插入

mysql - mysql查询后释放内存

php - 如何创建在 Windows 中运行 PHP 代码的快捷方式?

mysql - Oracle 的数据库链接允许用户在多个物理数据库上查询

php - 服务器安全图片

mysql - 如何从MySQL中的每个组中选择随机行?

mysql - 我该如何计算这个日期?

codeigniter - 将教义与CodeIgniter集成

javascript - 需要在 React Native 应用程序上实现 onPress 逻辑来香水两个任务

php - 在 PDF 文件中绘图