Welcome Guest, Not a member yet? Register   Sign In
Library playing me up!
#1

[eluser]Adam Griffiths[/eluser]
Hey!

I am writing an authentication library, firstly, here is my constructor.

Code:
function Auth($config)
    {
        $CI =& get_instance();
        $CI->load->library('session');
    }

I get this error, whenever I use $CI->function->parameter('value'); from any other function in the auth class.

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: CI

Filename: libraries/Auth.php

Line Number: 53


A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/Auth.php

Line Number: 53

Line 53 is...

Code:
$CI->load->library('session');

I am running PHP 5.2.5 The user guide only says that PHP 4 has problems returning the codeigniter instance from a constructor...I have also tried using __construct as my constructor name but it has the same end result.

All comments welcome.

Thanks Smile
#2

[eluser]usmc[/eluser]
Instead of $CI it should be $this->CI
#3

[eluser]Colin Williams[/eluser]
Code:
var $CI;

function Auth($config)
{
  $this->CI =& get_instance();
  $this->CI->load->library('session');
}
#4

[eluser]wiredesignz[/eluser]
[quote author="usmc" date="1218946950"]Instead of $CI it should be $this->CI[/quote]

Why should it be $this->CI if the super object is not required as a class variable?

Code:
$CI =& get_instance();    // is perfectly acceptable


@Colin, ditto, and you should know better.

Giving people assistance is one thing, at least ensure you give accurate information.
#5

[eluser]Colin Williams[/eluser]
@wiredesignz, Adam said

Quote:I get this error, whenever I use $CI->function->parameter(’value’); from any other function in the auth class.

Obviously the super object is required as a class variable, unless he wants $CI =& get_instance(); in every class member function. Where exactly was I inaccurate?
#6

[eluser]wiredesignz[/eluser]
There is nothing wrong with using $CI =& get_instance() in every method of a library if required and that should be made clear.
#7

[eluser]Adam Griffiths[/eluser]
@usmc: Thanks!

@Colin: Thanks for the code example! Big Grin

@wired: I know there is nothing wrong with having the $CI =& get_instance() in every function, it's just it gets tedious after a while, and I would much rather have it in one place.

Smile
#8

[eluser]gon[/eluser]
[quote author="wiredesignz" date="1218969592"][quote author="usmc" date="1218946950"]Instead of $CI it should be $this->CI[/quote]

Why should it be $this->CI if the super object is not required as a class variable?

Code:
$CI =& get_instance();    // is perfectly acceptable


@Colin, ditto, and you should know better.

Giving people assistance is one thing, at least ensure you give accurate information.[/quote]


IMHO, Colin's suggestion was perfectly right and accurate for solving the error. He didn't say that his was the only way to do that.
Indeed I agree that keeping a common reference for the object is the right thing to do. Much more clear.

And what if the answer had been wrong? Probably another one (not you) would have seen it and made a polite correction to it. I don't think people sends inaccurate responses deliberately.


EDIT: Very funny. You wrote a post just after this making recommendations on using private vars for storing the CI variable. I didn't read that you could call get_instance on every method of the object. Why didn't you give accurate info? :coolgrin:
#9

[eluser]wiredesignz[/eluser]
Thanks for your opinion gon. I'm sure it contributed a lot to this discussion.
#10

[eluser]usmc[/eluser]
[quote author="wiredesignz" date="1218991731"]Thanks for your opinion gon. I'm sure it contributed a lot to this discussion.[/quote]

@wiredesignz
This might be a better framework for you
http://www.rubyonrails.org/ as it thrives on arrogance.




Theme © iAndrew 2016 - Forum software by © MyBB