1

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

5
  • which class does your Controller extend from? CI_Controller? Commented Nov 6, 2015 at 9:06
  • When you load library try just making the first letter of class upper case only Varmatrixspecanimal.php and class Varmatrixspecanimal {} then $this->load->library('varmatrixspecanimal'); Commented Nov 6, 2015 at 9:10
  • when you load library in controller then used small letter of library name. like this..$this->load->library('varMatrixSpecanimal'); but first letter of library class name must be upper case. Commented Nov 6, 2015 at 9:12
  • Yes my controller extends CI_Controller Commented Nov 6, 2015 at 9:49
  • @wolfgang1983 I changed class definition this way class Varmatrixspecanimal {....} and renamed the filename to Varmatrixspecanimal.php<br/>Then I called $this->load->library('varmatrixspecanimal');..... but I got the same error Commented Nov 6, 2015 at 10:01

4 Answers 4

1

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'); 
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

sorry, parent::__construct();
0

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

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.