在 PHP 中覆盖父级的父级构造函数

发布于 2024-10-04 06:05:15 字数 377 浏览 2 评论 0原文

因此,我有一个 PHP 类:

class DB extends mysqli{
    public function __construct(
      {
       parent::__construct('localhost','user','password','db');
      }
}

我的问题是我想用一个新类覆盖该类,该新类使用不同的数据库用户执行更多特权的数据库操作。

class adminDB extends DB{
    public function __construct(
      {
       ??
      }
    }
}

我应该在这里做什么?

I have one PHP class thus:

class DB extends mysqli{
    public function __construct(
      {
       parent::__construct('localhost','user','password','db');
      }
}

My problem is that I want to override this class with a new one that performs more privileged database operations with a different db user.

class adminDB extends DB{
    public function __construct(
      {
       ??
      }
    }
}

What should I do here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗地喜欢 2024-10-11 06:05:15

无论如何,您应该将凭据传递给构造函数:

class DB extends mysqli {
    public function __construct($host, $user, $password, $db)
    {
        parent::__construct($host, $user, $password, $db);
    }
}

那么您不需要继承,您可以只使用:

$adminDb = new DB($adminHost, $adminUser, $adminPassword, $db);
$nonAdminDb = new DB($host, $user, $password, $db);

但如果您确实想要继承,您仍然可以这样做:

class AdminDB extends DB {
    public function __construct() {
        parent::__construct('adminhost','adminuser','adminpassword','db');
    }
}

You should pass the credentials to the constructor anyway:

class DB extends mysqli {
    public function __construct($host, $user, $password, $db)
    {
        parent::__construct($host, $user, $password, $db);
    }
}

Then you don't need inheritance you can just use:

$adminDb = new DB($adminHost, $adminUser, $adminPassword, $db);
$nonAdminDb = new DB($host, $user, $password, $db);

But if you really want inheritance you could still do this:

class AdminDB extends DB {
    public function __construct() {
        parent::__construct('adminhost','adminuser','adminpassword','db');
    }
}
微凉徒眸意 2024-10-11 06:05:15
<?php
    class mohona{
        public $name;
        public $age;
        public $fname;
        public $lname;
        public function __construct($cname,$cage,$cfname,$clname){
            $this->name=$cname;
            $this->age=$cage;
            $this->fname=$cfname;
            $this->lname=$clname;


        }
        public function getMohona(){
            echo "Full Name: ".$this->fname." ".$this->lname." ".$this->name."<br/>Age: ".$this->age."<br/>";
        }
    }
    class ibrahim extends mohona{
        public $relational_status;
        public $relation;
        public $contact;
        public function __construct($cname,$cage,$cfname,$clname,$crelational_status,$crelation,$ccontact){
            parent::__construct($cname,$cage,$cfname,$clname);
            $this->relational_status=$crelational_status;
            $this->relation=$crelation;
            $this->contact=$ccontact;
        }
        public function getIbrahim(){
            echo "Full Name: ".$this->fname." ".$this->lname." ".$this->name."<br/>Age: ".$this->age."<br/>"."Relational Status: ".$this->relational_status."<br/>Maritual Status: ".$this->relation."<br/>Contact Status: ".$this->contact;
        }
    }
    $oMohona=new mohona("Mohona","20","Nafis","Anjum");
    $oIbrahim=new ibrahim("Ibu","25","Ibrahim","Akbar","Single","Unmarried","blocked");
    echo $oMohona->getMohona();
    echo $oIbrahim->getIbrahim();
?>
<?php
    class mohona{
        public $name;
        public $age;
        public $fname;
        public $lname;
        public function __construct($cname,$cage,$cfname,$clname){
            $this->name=$cname;
            $this->age=$cage;
            $this->fname=$cfname;
            $this->lname=$clname;


        }
        public function getMohona(){
            echo "Full Name: ".$this->fname." ".$this->lname." ".$this->name."<br/>Age: ".$this->age."<br/>";
        }
    }
    class ibrahim extends mohona{
        public $relational_status;
        public $relation;
        public $contact;
        public function __construct($cname,$cage,$cfname,$clname,$crelational_status,$crelation,$ccontact){
            parent::__construct($cname,$cage,$cfname,$clname);
            $this->relational_status=$crelational_status;
            $this->relation=$crelation;
            $this->contact=$ccontact;
        }
        public function getIbrahim(){
            echo "Full Name: ".$this->fname." ".$this->lname." ".$this->name."<br/>Age: ".$this->age."<br/>"."Relational Status: ".$this->relational_status."<br/>Maritual Status: ".$this->relation."<br/>Contact Status: ".$this->contact;
        }
    }
    $oMohona=new mohona("Mohona","20","Nafis","Anjum");
    $oIbrahim=new ibrahim("Ibu","25","Ibrahim","Akbar","Single","Unmarried","blocked");
    echo $oMohona->getMohona();
    echo $oIbrahim->getIbrahim();
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文