Too few arguments to function Db::__construct(), 0 passed in

后端 未结 1 1341
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 03:39

i have 3 pages one for connect the DB

class Db{

private $dbUserName =\"root\";
private $dbName = \"oop\";
private $dbPas = \"\";
private $dbHost = \"127.0.0         


        
相关标签:
1条回答
  • 2020-12-07 04:45

    Your base class constructor expects 4 arguments:

    class Db{
    
    ...
    
        public function __construct($dbUserName, $dbName, $dbPas, $dbHost)
        {
            ...
        }
    

    So you can not create the child class without parameters. You need to either do:

    $users = new ViewUser('root', 'dbname', 'pass', 'host');
    

    Or put defaults into constructor declaration:

    public function __construct($dbUserName='root', $dbName='db', $dbPas='pass', $dbHost='host')
    {
        ...
    }
    

    But in general, the inheritance is misused here. You'd better have a separate Db class to manage DB and use it as a component inside your other classes (use composition instead of inheritance).

    0 讨论(0)
提交回复
热议问题