I have one PHP class thus:
我有一个PHP类:
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.
我的问题是,我想用一个新的类来覆盖这个类,这个类可以对不同的db用户执行更多的特权数据库操作。
class adminDB extends DB{
public function __construct(
{
??
}
}
}
What should I do here?
我该怎么做呢?
2 个解决方案
#1
7
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');
}
}
#2
-1
<?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();
?>
#1
7
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');
}
}
#2
-1
<?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();
?>