0

I've got a helper file that requires accessing some session data and so it looks like this (this isn't the actual file contents, but rather a simplified version with the same level of necessary detail):

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

$CI =& get_instance();

if (!is_null($CI->session->userdata('name')) {
    if ( ! function_exists('sayHi'))
    {
        function sayHi() {
            echo "Hello, " . $CI->session->userdata('name') . "!";
        }
    }
}

So let's say I'm looking at pages/home and this function is called when I submit to the same page - it's called by the model. No errors when the function is not called. When the function is called, it gives me this:

Message: Undefined variable: CI

What's going on?

0

2 Answers 2

1

It all about scope, the function doesn't see that variable.

Put this in the function body: $CI =& get_instance();.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow, I feel like a moron. I knew about this, but I guess I just didn't recall it when I saw this error. Dang. Thanks a bunch!
1

$CI is not in the visibility scope of your function code. This is a basic PHP thing, read more here: http://www.php.net/manual/en/language.variables.scope.php

1 Comment

Oh wow, I feel like a moron. I knew about this, but I guess I just didn't recall it when I saw this error. Dang. Thanks a bunch!

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.