简体   繁体   English

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

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

i have 3 pages one for connect the DB我有 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 ;
}

the 2 is 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;
    }

}

}

the 3 is 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>";
    }

}

}

when I try to run them on another page:当我尝试在另一个页面上运行它们时:

<?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>

i get a error:我收到一个错误:

Uncaught ArgumentCountError: Too few arguments to function Db::__construct(), 0 passed in /Users/mohamedelmasry/Documents/websites/oop/test.php on line 18 and exactly 4 expected in /Users/mohamedelmasry/Documents/websites/oop/Db.php:18 Stack trace: #0 /Users/mohamedelmasry/Documents/websites/oop/test.php(18): Db->__construct() #1 /Users/mohamedelmasry/.composer/vendor/laravel/valet/server.php(128): require('/Users/mohamede...') #2 {main} thrown in /Users/mohamedelmasr未捕获的 ArgumentCountError:函数 Db::__construct() 的参数太少,第 18 行在 /Users/mohamedelmasry/Documents/websites/oop/test.php 中传入 0,而在 /Users/mohamedelmasry/Documents/websites/oop 中预期正好为 4 /Db.php:18 堆栈跟踪:#0 /Users/mohamedelmasry/Documents/websites/oop/test.php(18): Db->__construct() #1 /Users/mohamedelmasry/.composer/vendor/laravel/valet /server.php(128): require('/Users/mohamede...') #2 {main} 扔在 /Users/mohamedelmasr

Your base class constructor expects 4 arguments:您的基类构造函数需要 4 个参数:

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).您最好有一个单独的Db类来管理 DB 并将其用作其他类中的组件(使用组合而不是继承)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel:函数App \\ Exports \\ JobExport :: __ construct()的参数太少,传递了0 - Laravel: Too few arguments to function App\Exports\JobExport::__construct(), 0 passed 函数 App\\Models\\Task::__construct() 的参数太少,第 26 行传递了 0,正好是 1 - Too few arguments to function App\Models\Task::__construct(), 0 passed on line 26 and exactly one 致命错误:未捕获的 ArgumentCountError:函数 AmazonAPI::construct() 的参数太少,通过了 0 - Fatal error: Uncaught ArgumentCountError: Too few arguments to function AmazonAPI::construct(), 0 passed 太少 arguments 到 function App\Exports\NilaiExport::__construct(), 0 传入 NilaiController.php 和第 112 行的预期值 - Too few arguments to function App\Exports\NilaiExport::__construct(), 0 passed in NilaiController.php on line 112 and exactly 1 expected 致命错误:未捕获的 ArgumentCountError:函数 Class::__construct() 的参数太少,传入 0 并且在 PHP7.1 中预期至少为 1 - Fatal error: Uncaught ArgumentCountError: Too few arguments to function Class::__construct(), 0 passed in and at least 1 expected in PHP7.1 函数Slim \\ Views \\ TwigExtension :: __ construct()的参数太少,传递了0个且恰好期望2个 - Too few arguments to function Slim\Views\TwigExtension::__construct(), 0 passed and exactly 2 expected (E_ERROR)函数Symfony \\ Component \\ HttpKernel \\ Profiler \\ Profile :: __ construct()的参数太少,已传递0 - (E_ERROR) Too few arguments to function Symfony\Component\HttpKernel\Profiler\Profile::__construct(), 0 passed arguments 到 function Mautic\PluginBundle\Integration\AbstractIntegration::__construct() 太少,通过了 0,预期正好 16 - Too few arguments to function Mautic\PluginBundle\Integration\AbstractIntegration::__construct(), 0 passed and exactly 16 expected Laravel:arguments 到 function 0 已通过且 1 预期太少 - Laravel: Too few arguments to function 0 passed and 1 expected Symfony DI:未捕获的 ArgumentCountError:函数 App::__construct() 的参数太少,在第 28 行传入 index.php 的参数为 0,预期为 1 - Symfony DI: Uncaught ArgumentCountError: Too few arguments to function App::__construct(), 0 passed in index.php on line 28 and exactly 1 expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM