开发者

PHP Child class accessing object in Parent class

开发者 https://www.devze.com 2023-02-12 01:43 出处:网络
class bm_main { public $db; public function __construct(){ $this->db = n开发者_如何转开发ew db();
class bm_main {

    public $db;

    public function __construct(){

        $this->db = n开发者_如何转开发ew db();
    }

}

class bm extends bm_main{

    public function __construct($id){
        $this->db = parent::$db;
            $this->db->save($id);
    }
}

How to access the $db object from parent class so i can use it in the child one


Call the parent constructor so the db class is instantiated:

    public function __construct($id) {
        parent::__construct();
        $this->db->save($id);
    }

The $db property is inherited by the subclass, and is public, so you can access it using $this->db.

0

精彩评论

暂无评论...
验证码 换一张
取 消