agodoo
agodoo

Reputation: 539

CodeIgniter: error loading custom library

I created a custom Library in CodeIgniter, and positioned it in 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;
    }
}
?>

Then in one controller (application/controllers/certificate.php) method I added these two lines of code:

public function save1()
{
    //..... some more code before

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

But when I call save1 method, I get this error:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Certificate::$varmatrixspecanimal
Filename: controllers/certificate.php
Line Number: 139

I don't understand where I do wrong, please help me. I checked also CodeIgniter help http://www.codeigniter.com/user_guide/general/creating_libraries.html but I was not able to get my error

Upvotes: 1

Views: 1292

Answers (4)

Try to change your constructor from this:

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

to this:

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

Also, when loading your library i think you don't need to uppercase any letters

Upvotes: 0

ZenithS
ZenithS

Reputation: 1007

Add include file with correct path at the top of program will solve this issue.

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

Upvotes: 0

Arvind Jaiswal
Arvind Jaiswal

Reputation: 442

Add get_instance() function in constructor in VarMatrixSpecanimal.php

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

}

After this load library on your controller

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

Upvotes: 1

A48
A48

Reputation: 29

I have experienced anything like this because I forgot this line of code: parent::__construct in my __construct or constructor.

Upvotes: 0

Related Questions