【问题标题】:exists, but doesn't declare class Code Igniter存在,但不声明类 Code Igniter
【发布时间】:2020-03-17 16:16:31
【问题描述】:

我尝试加载这个名为“m_cart”的模型,但它在我的控制器上不起作用。我在这里遇到错误“M_cart.php 存在,但没有声明类 M_cart”

  1. Controller Part.php
    <?php

class Cart extends CI_Controller{

    function __construct(){
        parent::__construct();
        $this->load->model('M_cart');
        $this->load->session('cart');
    }
  1. 模型 m_cart.php
     <?php
    class Cart_model extends CI_Model{

        function get_all_produk(){
            $hasil=$this->db->get('produk');
            return $hasil->result();
        }    

【问题讨论】:

    标签: php function codeigniter class


    【解决方案1】:
    1. 如果你声明class Cart,控制器名称应该是Cart.php而不是Part.php(不要忘记扩展CI_Controller)。
    2. m_cart 中的 m 在文件名 M_cart.php 和声明中都应大写,并应扩展 CI_Model,例如class M_cart extends CI_Model

    如果两者都无法正常工作,请验证验证验证您正在编辑与测试 .etc 相同的副本。

    【讨论】:

      【解决方案2】:

      您的模型必须声明为扩展 CI_Model 的类

      Class M_cart extends CI_Model
      {
      
          function get_all_produk()
          {
              $hasil=$this->db->get('produk');
              return $hasil->result();
          }
      
          function some_other_model()
          {
              // some other thing
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多