【问题标题】:access the function of my library访问我的图书馆的功能
【发布时间】:2018-06-18 13:32:15
【问题描述】:

当我尝试访问我的库的功能时遇到此错误: 遇到 PHP 错误

严重性:通知

消息:未定义的属性:ControllerUser::$alertClasse

文件名:控制器/ControllerUser.php

行号:183

回溯:

文件: C:\wamp64\www\logistock\application\controllers\ControllerUser.php 行:183 函数:_error_handler

文件: C:\wamp64\www\logistock\application\controllers\ControllerUser.php 行:22 函数:call_user_func_array

文件:C:\wamp64\www\logistock\index.php 行:315 功能: 需要一次

这是我的图书馆:

defined('BASEPATH') OR exit('No direct script access allowed');

class AlertClasse
{
    protected $CI;

    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->model('AlerteModel');

    }

    public function stockSeuil()
    {
        $query=$this->CI->AlerteModel->getarticles();
        $tabStockSeuil = array();
        foreach($query as $tab)
        {

            if($tab->art_solde<=$tab->seuil_alert_art)
            {
              $article[] = $tab->id_art;
              $article[] = $tab->lib_art;
              $article[] = $tab->art_solde;
              $article[] = $tab->seuil_alert_art;   
              $article[] = $tab->perissable;        
              $tabStockSeuil[] = $article;
              $article = array();
            }
        }
        return $tabStockSeuil;
    }

    public function stockExpire()
    {
        $query=$this->CI->AlerteModel->get_art_lot();
        $tabartexpire = array();
        $datejour = date('d/m/Y');

         $djour = explode("/", $datejour);

        $auj = mktime(00, 00, 00,$djour["1"],$djour["0"],$djour["2"]);


        foreach($query as $tab)
        {

            if($tab->date_expir!=0)
            {

            $datexpiration= $tab->date_expir;  

            if ($auj>=$datexpiration)
            { 
                $id = $tab->id_lot;
                $artexpire[] = $tab->lib_art;
                $artexpire[] = $tab->qte_lot;
                $artexpire[] = $tab->date_enregistrement;
                $artexpire[] = $tab->date_expir;

                $tabartexpire[] =$artexpire;
                $this->articles_model->mis_au_rebut($id);
                $artexpire = array();

            }
        }

            }
          return $tabartexpire;
    } 


    public function ExpireProche()
    {
        $query=$this->CI->AlerteModel->get_art_lot();
        $tabartexpire = array();
        $datejour = date('d/m/Y');

         $djour = explode("/", $datejour);

        $auj =  mktime(00, 00, 00, $djour["1"], $djour["0"], $djour["2"]);

        $nextDat =time() + (5 * 24 * 60 * 60);
        $temp =date('d/m/Y', $nextDat);
        $temp = explode("/", $temp);  

        $nextDate  = mktime(00, 00, 00, $temp["1"],$temp["0"],$temp["2"]);    
        foreach($query as $tab)
        {
            if($tab->date_expir!=0)
            {

            $datexpiration= $tab->date_expir; 



          if (intval($auj)<intval($datexpiration) && intval($nextDate) >= 
        intval($datexpiration))
            { 

                $artexpire[] = $tab->lib_art;
                $artexpire[] = $tab->qte_lot;
                $artexpire[] = $tab->date_enregistrement;
                $artexpire[] = $tab->date_expir;
                // On met ensuite le résultat obtenu dans un tableau 
                $tabartexpire[] =$artexpire;
                $artexpire = array();

         }
        }


            }

            return $tabartexpire;


    }
}

在我的控制器中:

defined('BASEPATH') OR exit('No direct script access allowed');

class ControllerUser extends CI_Controller {

    function __construct() {

        parent::__construct();
        $this->load->library('alertClasse');
        $this->load->database();
        $this->load->library('session');
    }
}

这里我尝试访问该方法:

public function affichageUser(){
   //error here
    $tabExpirProch = $this->alertClasse->ExpireProche();
    $priv = $this->UserModel->getpriv();
    $this->load->view('users/listUser',['privilege'=>$priv]);
}

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    我的项目也有同样的问题。 我可以在 CI v3.1.6 上以这种方式调用每个模型

    模型(仅显示差异):

    class SomeClass extends CI_Model{
        function __construct(){
            parent::__construct()
        }
        function someFunction($foo){
            //do something
        }
    }
    

    在控制器中:

    class SomeController extends CI_Controller{
        function __construct(){
            parent::__construct();
            //Load model, no problem
            $this->load->model('SomeClass');
            //Load library, no problem
            $this->load->library('MyLibrary');
        }
        function someFunctionToUseModelFunction(){
            $foo = 'Hello my friend';
            //no problem with using the model
            $bar = $this->SomeClass->SomeFunction($foo);
            //library, NOT FOUND
            $a = $this->MyLibrary->someFunctionInLibrary();
        }
    }
    

    结果当我尝试使用库时,当我需要调用库中的函数时,我不能,检查名称,文件名,我在哪里调用这些......什么都没有。 (对不起我的英语)我“小写”了图书馆的名字,我这样做了

    function __construct(){
        $this->load->library('MyLibrary');
    }
    
    function functionToCallTheLbrary(){
        //this way it has worked for my so far with all the libraries i have
        //lowercase the class name, and call the functions with the names u declare those
        $var = $this->mylibrary->someFunction();
    }
    

    希望我的回答对你有所帮助。

    【讨论】:

      【解决方案2】:

      您必须使用类名访问库的方法:

      $tabExpirProch = $this->AlertClasse->ExpireProche();
      

      【讨论】:

      • 我就是这么做的
      【解决方案3】:

      发生此错误是因为 codeigniter 无法在您的应用程序/库文件夹中找到该文件。

      确保你的库类的文件名是“AlertClasse.php” 因为 codeigniter 规定了以下创建库的规则:

      1. 文件名必须大写。例如:Myclass.php
      2. 类声明必须大写。例如:类 Myclass
      3. 类名和文件名必须匹配。

      https://www.codeigniter.com/userguide3/general/creating_libraries.html#storage

      【讨论】:

        猜你喜欢
        • 2020-12-16
        • 1970-01-01
        • 1970-01-01
        • 2015-02-03
        • 1970-01-01
        • 2021-10-04
        • 2012-05-04
        • 2016-03-16
        • 2015-06-03
        相关资源
        最近更新 更多