【问题标题】:The PHP file does not find a class, but the class is definedPHP文件没有找到类,但是定义了类
【发布时间】:2018-12-15 03:56:02
【问题描述】:

我有一个名为“purchases.controller.php”的 php 文件,其中在 'Purchases' 类的一个名为 'ctrCash' 的函数中,我将变量传递给我在 'CartController' 类的一个名为 'ctrNewCashPurchase' 的函数已经定义,但是当我运行项目时,我收到消息:

“致命错误:未捕获的错误:在...中找不到类'CartModel'”

如果我在函数 ctrNewCashPurchase 中执行 var_dump,我意识到我正在输入该函数,但它告诉我它无法识别“CartModel”,我不明白为什么。

我分享“purchases.controller.php”文件的代码:

class CartController{

    static public function ctrNewCashPurchase($datos){

        $tabla = "compras";

        $respuesta = CartModel::mdlNewCashPurchase($tabla, $datos);

        if($respuesta == "ok"){

            $tabla = "comentarios";
            ModeloUsuarios::mdlIngresoComentarios($tabla, $datos);

        }

        return $respuesta;

    }      

}

class Purchases {

    public function ctrCash (&$arrayCompleto, &$usuario, &$direccion1, &$direccion2, &$dia, &$hora, &$email, &$telefono, &$sesion){

        if(isset($usuario)){

             //Here I create an array
             for($i = 0; $i < count($arrayCompleto); $i++){

               $datos = array("idUsuario"=> $sesion,
                              "idProducto"=> $arrayCompleto[$i]["idProducto"],
                              "metodo"=> "Efectivo",
                              "email"=> $email,
                              "direccion"=> $direccion1,
                              "detalleDireccion"=> $direccion2,
                              "diaEnvio"=> $dia,
                              "horaEnvio"=> $hora,
                              "telefono"=> $telefono,
                              "pais"=> "ARG");

                }

             $respuesta = CartController::ctrNewCashPurchase($datos);

          }

     }

}

我分享了“purchases.model.php”文件的代码,我在其中定义了 CartModel 类:

class CartModel{

    static public function mdlNewCashPurchase($tabla, $datos){

        $stmt = Conexion::conectar()->prepare("INSERT INTO $tabla (id_usuario, id_producto, metodo, email, direccion, pais, detalleDireccion, diaEnvio, horaEnvio, telefono) VALUES (:id_usuario, :id_producto, :metodo, :email, :direccion, :pais, :detalleDireccion, :diaEnvio, :horaEnvio, :telefono)");

        $stmt->bindParam(":id_usuario", $datos["idUsuario"], PDO::PARAM_INT);
        $stmt->bindParam(":id_producto", $datos["idProducto"], PDO::PARAM_INT);
        $stmt->bindParam(":metodo", $datos["metodo"], PDO::PARAM_STR);
        $stmt->bindParam(":email", $datos["email"], PDO::PARAM_STR);
        $stmt->bindParam(":direccion", $datos["direccion"], PDO::PARAM_STR);
        $stmt->bindParam(":pais", $datos["pais"], PDO::PARAM_STR);
        $stmt->bindParam(":detalleDireccion", $datos["detalleDireccion"], PDO::PARAM_STR);
        $stmt->bindParam(":diaEnvio", $datos["diaEnvio"], PDO::PARAM_STR);
        $stmt->bindParam(":horaEnvio", $datos["horaEnvio"], PDO::PARAM_STR);
        $stmt->bindParam(":telefono", $datos["telefono"], PDO::PARAM_INT);

        if($stmt->execute()){ 

            return "ok"; 

        }else{ 

            return "error"; 

        }

        $stmt->close();

        $tmt =null;
    }

}

我添加了另一个名为“aux.php”的文件,以防它影响导致我的错误中的某些内容。以下是如何在“ctrCash”函数中发送“purchases.controller.php”参数

    if(isset($_POST['usuario'])){

    require ('purchases.controller.php');

    $arrayCompleto = json_decode($_POST['arrayCompleto'], true);

$usuario = $_POST['usuario']; 
$direccion1 = $_POST['direccion1']; 
$direccion2 = $_POST['direccion2']; 
$dia = $_POST['dia']; 
$hora = $_POST['hora']; 
$email = $_POST['email']; 
$telefono = $_POST['telefono']; 
$sesion = $_POST['sesion'];


 $payments = new Purchases(); 
$payments -> ctrCash($arrayCompleto, $usuario, $direccion1, $direccion2, $dia, $hora, $email, $telefono, $sesion);

    }

【问题讨论】:

  • 你有没有在某处包含_once模型?
  • @Shim-Sao 不。我在另一个名为 'aux.php' 的文件中所做的唯一一件事(现在我将它添加到我的问题的末尾)是 require 'purchases.controller.php '。我不知道这是否会影响任何事情。 3 分钟后,您将在出版物末尾看到它。
  • @Shim-Sao 我已经添加了其他信息

标签: php


【解决方案1】:

错误是因为没有为控制器导入模型。

您可以将它包含在控制器中,它是相同的(在这种情况下)。

if(isset($_POST['usuario'])){

require_once ('purchases.model.php');
require_once ('purchases.controller.php');
...

}

【讨论】:

  • 如果我把模型放在你刚才告诉我的地方,也就是'aux.php'里面,就会出现这个错误:Warning: require(purchases.model.php): failed to open stream: No这样的文件或目录...
  • 不会发生此类文件或目录错误,因为 purchase.model.php 可能位于其他目录之外的其他目录中。您必须提供文件的相对或完整路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
  • 2020-02-25
  • 2023-04-05
  • 2012-09-28
  • 1970-01-01
  • 2019-10-19
相关资源
最近更新 更多