Welcome Guest, Not a member yet? Register   Sign In
Error creating new libraries
#1

[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?
#2

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

[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();
#4

[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.
#5

[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());
#6

[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.
#7

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

[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?
#9

[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
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB