CodeIgniter:加载自定义库时出错


CodeIgniter: error loading custom library

我在CodeIgniter中创建了一个自定义库,并将其定位在application/libraries/VarMatrixSpecanimal.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class VarMatrixSpecanimal {
    protected $variabiliMatrix;
    public function __construct() {
        $variabiliMatrix['cat']['no']=1;
        $variabiliMatrix['dog']['a']=2;
        $variabiliMatrix['bird']['b']=3;
    }
    public function get_matrix() {
        return $this->variabiliMatrix;
    }
}
?>

然后在一个控制器(application/controllers/certificate.php)方法中,我添加了这两行代码:

public function save1()
{
    //..... some more code before
    $this->load->library('VarMatrixSpecanimal');
    $numerical_values = $this->varmatrixspecanimal->get_matrix();
    //..... some more code after

但是当我调用save1方法时,我得到了这个错误:

遇到PHP错误
严重性:通知
消息:未定义的属性:证书::$varmatrixspecanimal
文件名:controllers/certificate.php
行号:139

我不明白我哪里做错了,请帮帮我。我还查看了CodeIgniter的帮助http://www.codeigniter.com/user_guide/general/creating_libraries.html但我无法获得我的错误

在VarMatrixSpecanimal.php 的构造函数中添加get_instance()函数

public function __construct()
{
    $this->variabiliMatrix =& get_instance();
}

在您的控制器上加载此库之后

$this->load->library('varmatrixspecanimal'); 

我经历过这样的事情,因为我忘记了这行代码:parent::__construct在我的__construct或构造函数中。

在程序顶部添加路径正确的include文件将解决此问题。

include "../../VarMatrixSpecanimal.php";

尝试更改您的构造函数:

public function __construct() {
    $variabiliMatrix['cat']['no']=1;
    $variabiliMatrix['dog']['a']=2;
    $variabiliMatrix['bird']['b']=3;
}

到此:

public function __construct() {
    $this->variabiliMatrix['cat']['no']=1;
    $this->variabiliMatrix['dog']['a']=2;
    $this->variabiliMatrix['bird']['b']=3;
}

此外,当加载库时,我认为您不需要大写任何字母