调用函数有时不起作用

Calling to a function doesn't work at times

在我目前正在进行的项目中,我想从 class 中的函数获取数据。因此我从我的主索引文件中调用该函数。但是在我最初想拨打电话的地方出现以下错误:

Fatal error: Uncaught Error: Call to a member function searchKlant() on array in N:\xampp\htdocs\Sylvia project\index.php:66 Stack trace:

0 {main} thrown in N:\xampp\htdocs\Sylvia project\index.php on line 66

但是,如果我使用同一行代码:

$klantfor = $klant->searchKlant($_GET['k']);

在同一代码的不同位置,我确实得到了所需的结果。 如果我调用相同的函数但这次在不同的 class 它确实有效,那么它会变得更加有趣。

如果有人能帮助我解决问题,我将不胜感激。 提前谢谢你

主页面代码:

<?php
spl_autoload_register(function ($class_name) {
    include 'custom/' . $class_name . '.class.php';
});

$klant = new Klant;
$invoice = new Invoice;

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['form'])){
    if($_POST['form']=="klant") {
       $klant->addKlant($_POST);
    } else if($_POST['form']=="invoice"){
       $invoice->addInvoice($_POST);
    }
}

$allklanten = $klant->getKlanten();
$allinvoice = $invoice->getInvoice();


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
    <meta charset="UTF-8">
</head>
<body>
    <?php include("components/header.php") ?>

    <div class="row">

        <div class="col-md-2">
            <ul class="col-sm-4 list-unstyled">
                <?php
                    foreach($allklanten as $klant){
                        echo "<li><a href='?page=0&k=" . $klant['kID'] . "'>" . $klant['kVoornaam'] . " " . $klant['kAchter'] . "</a></li><br>";
                    }
                ?>
            </ul>
        </div>

        <?php
            if(isset($_GET['page'])) {
                if ($_GET['page'] == 2) {
                    include("pages/klantadd.php");
                } else if ($_GET['page'] == 3) {
                    include("pages/invoiceadd.php");
                }
            }
        ?>

        <div class="col-md-8">
            <?php
                if(isset($_GET['k'])){

                    $klantfor = $klant->searchKlant($_GET['k']);
                    $invoicefor = $invoice->searchInvoice($_GET['k']);

                    foreach ($klantfor as $klant){
                    echo "<h3>" . $klant['kVoornaam'] . " " . $klant['kAchter'] . "</h3>";}

                    echo "<table class=\"table table-hover\">
                            <thead>
                              <tr>
                                <th>ID</th>
                                <th>Datum</th>
                                <th>test.class</th>
                              </tr>
                            </thead>
                            <tbody>
                          ";

                    foreach($invoicefor as $invoice){
                        echo "<tr>";
                        echo "<td>" . $invoice['iID'] . "</td>";
                        echo "<td>" . $invoice['iDate'] . "</td>";
                        echo "</tr>";
                    }

                    echo "</tbody></table>";
                }
            ?>
        </div>

    </div>
</body>

有问题的功能:

public function searchKlant($target){
        $db = new Database();
        $db = $db->getConn();

        $statement = $db->prepare("SELECT * FROM klant WHERE kID = ?");
        $statement->bind_param("i",$target);
        $statement->execute();
        $result = $statement->get_result();
        while ($row = $result->fetch_assoc()) {
            $klant[] = $row;
        }
        $statement->close();
        $db->close();

        return $klant;
    }

您在此处覆盖 class Klant 的实例 $klant

foreach($allklanten as $klant) {....

所以将其更改为其他内容($klantItem 左右)。

看这个简单的例子:

<?php

$array = Array(1,2,3);

$v="a class";
foreach($array as $v) {
    echo $v;
}

echo "outside: ".$v;
// output: 'outside: 3' - so "a class" is overwritten.