函数Db :: __ construct()的参数太少,传入0

时间:2017-05-19 13:48:17

标签: php oop

我有3页用于连接数据库

class Db{

private $dbUserName ="root";
private $dbName = "oop";
private $dbPas = "";
private $dbHost = "127.0.01";

public function __construct($dbUserName, $dbName, $dbPas, $dbHost)
{
    $this->dbUserName = $dbUserName;
    $this->dbName = $dbName;
    $this->dbPas = $dbPas;
    $this->dbHost = $dbHost;

    $con = new PDO("mysql:host= $dbHost; $dbUserName,$dbName,$dbPas");
    return $con ;
}

2是

class User extends Db{
protected function getAllUser(){

    $sql = "SELECT * FROM user";
    $result = $this->connect()->query($sql);

    $count = $result->rowCount();
    // check if there'r date in the db
    if($count > 0){
        while($row = $result->fetchAll() ){
            $data[] = $row;
        }
        return $data;
    }

}

}

3是

class ViewUser extends User{

public function ViewAllUser (){

    $datas = $this->getAllUser();

    foreach($datas as $data){
        //echo the db rows
        echo $data['uid']."</br>";
        echo $data['pas']."</br>";
    }

}

}

当我尝试在其他页面上运行它们时

<?php
include 'Db.php';
include 'User.php';
include 'ViewUser.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Oop</title>
</head>
<body>

     <?php
        $users = new ViewUser();
        $users->ViewAllUser();
     ?>

</body>
</html>

我收到错误Uncaught ArgumentCountError:函数Db :: __ construct()的参数太少,0在第18行的/Users/mohamedelmasry/Documents/websites/oop/test.php中传递,而在/ Users /中恰好是4 mohamedelmasry / Documents / websites / oop / Db.php:18 Stack trace:#0 /Users/mohamedelmasry/Documents/websites/oop/test.php(18):Db-&gt; __ construct()#1 / Users / mohamedelmasry / .composer / vendor / laravel / valet / server.php(128):require(&#39; / Users / mohamede ...&#39;)/ users / mohamedelmasr中抛出#2 {main}

1 个答案:

答案 0 :(得分:2)

您的基类构造函数需要4个参数:

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dao.RegistrationDAO;
import com.vo.RegistrationVO;

@RestController
public class RegistrationController {

    @Autowired
    private RegistrationDAO registrationDAO;

    @RequestMapping(value = { "/", "/index.html" })
    public String load() {
        return "angularForm";
    }

    @RequestMapping(value = "/insert.html", method = RequestMethod.POST , consumes = MediaType.APPLICATION_JSON_VALUE)
    public void insert(@RequestBody RegistrationVO registrationVO) {
        System.out.println("------");
        this.registrationDAO.insert(registrationVO);
    }
}

因此,您无法在没有参数的情况下创建子类。 你需要做:

class Db{

...

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

或者将默认值放入构造函数声明中:

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

但总的来说,继承在这里被滥用了。您最好有一个单独的public function __construct($dbUserName='root', $dbName='db', $dbPas='pass', $dbHost='host') { ... } 类来管理数据库并将其用作其他类中的组件(使用组合而不是继承)。