CodeIgniter Forums
Error creating new libraries - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Error creating new libraries (/showthread.php?tid=50488)

Pages: 1 2


Error creating new libraries - El Forum - 03-28-2012

[eluser]Marian Alexandru[/eluser]
I have a library call func.php:

Code:
class Func {

var $CI;

public function __construct() {
  $this->CI = & get_instance();
  $this->CI->load->library(array('form_validation', 'session'));
  $this->CI->lang->load('ro', 'romanian');
  $this->CI->load->model('m_logat');
}

public function content() {
  $this->CI->load->view('logat/header', $data);
}
}

When I call func->content() it get me this error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Logat::$CI

Filename: libraries/func.php

Line Number: 40

If a write

Code:
public function content() {
$this->CI = & get_instance();
$this->CI->load->view('logat/header', $data);
}

I don't get that error. So my question is: I'm obligated to use $this->CI = & get_instance(); in every function if I want to load a view?


Error creating new libraries - El Forum - 03-28-2012

[eluser]CodeIgniteMe[/eluser]
Code:
$this->CI = & get_instance();
is different from
Code:
$this->CI =& get_instance();
Creating Libraries


Error creating new libraries - El Forum - 03-28-2012

[eluser]Marian Alexandru[/eluser]
If I put

Code:
$this->CI =& get_instance();

I don't resolv the problem.

And

Code:
$this->CI = & get_instance();

isn't different from

Code:
$this->CI =& get_instance();



Error creating new libraries - El Forum - 03-28-2012

[eluser]PhilTem[/eluser]
It's odd. I never had problems with libraries and a reference to the CI super-object by using the same code you did. But let's try to narrow down on the source of your problem. I just got some questions:

- Which PHP version are you running on?
- Does it work if you replace
Code:
var $CI
with
Code:
public $CI
? Even though it's actually the same.
- Try to use it statically with
Code:
static $CI
and
Code:
self::$CI =& get_instance();
- Turn on logging (set logging_threshold to 4) and have a look at the logs, maybe they can help a little more.


Error creating new libraries - El Forum - 03-28-2012

[eluser]Narf[/eluser]
Your code is perfectly fine and nothing mentioned so far (except for the PHP version in use) can be related to the error you're getting.

The problem is that your constructor isn't being executed on class (library) instantiation and that's indeed a CI bug. Until the fixed version is released, you can load your library with dummy (but non-NULL) parameters passed to it, like this:

Code:
$this->load->library('func', array());



Error creating new libraries - El Forum - 03-28-2012

[eluser]Marian Alexandru[/eluser]
My php version is 5.3.8.

Thx PhilTem, with your code I solve the problem.

If I put

Code:
$this->load->library('func', array());

I still get that error.


Error creating new libraries - El Forum - 03-28-2012

[eluser]Narf[/eluser]
Eh ... if that fixes your problem - you're deffinately not showing all the code. Smile


Error creating new libraries - El Forum - 03-28-2012

[eluser]PhilTem[/eluser]
I'm confused, too. The number of lines didn't match the error-log, too (something with an error on line 40, even though the posted code only has like 15 lines).

@Marian Which part solved your problem? Using 'static' or telling PHP $CI is public rather than just a var?


Error creating new libraries - El Forum - 03-28-2012

[eluser]Marian Alexandru[/eluser]
Here are the first 40 lines of library

Code:
<?php

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

/*
* Fisierul cu functiile principale din joc
*/

class Func {

static $CI;

public function __construct($param = '') {
  self::$CI = & get_instance();
  self::$CI->load->library(array('form_validation', 'session'));
  self::$CI->lang->load('ro', 'romanian');
  self::$CI->load->model('m_logat');
}

public function user_id() {
  return $this->session->userdata('user');
}

public function town_id() {
  return $this->session->userdata('town');
}

public function msg($msg = '') {
  $data["mesaj"] = $msg;
  func::content("msg", $data);
}

public function content($fisier = '', $date = '', $info = 0) {
  //$CI = & get_instance(); // :|
  $data = array(
   "new_message" => $this->m_logat->mesaj_n(func::user_id()),
   "new_raport" => $this->m_logat->raport_n(func::user_id())
  );
  self::$CI->load->view('logat/header', $data);

I call the func->content() from a controller named logat.php


Error creating new libraries - El Forum - 03-28-2012

[eluser]Narf[/eluser]
Well, it would've worked if you called $this->CI->m_logat, instead of $this->m_logat.