Welcome Guest, Not a member yet? Register   Sign In
Newbie: Fatal error: Using $this when not in object context - URI Helper.

When the CI User Guide states:
Quote:Note: This class is initialized automatically by the system so there is no need to do it manually.

What does this mean? It means that we don't need to create an instance of a class?

I was trying to use the following on my View:

I'm getting an error:
Quote:Using $this when not in object context in /home/akasite/private/application/views/v_categoria.php on line 111

What's the issue?


I believe this due the fact that we are trying to access $this inside a function (on our view). But why can't we?


[eluser]GSV Sleeper Service[/eluser]
because vars within functions are not global, unless you use the 'global' keyword.
$foo = 'donkey';
function bar(){ return $foo; }
function bar2(){ global $foo; return $foo; }
echo bar(); //will produce an error
echo bar2(); //will echo 'donkey'
if you want to use $this in a function then you'll have to either make it global (bad!) or pass in a reference to $this as a parameter to your function. have a good think about what you're doing, I'm sure there's a cleaner solution.

Thanks. In fact CI allows us to do this by it's own methods right?

So, I guess I have to forget about passing this on a view, and try to re-structure the all thing to be CI accordingly and use the controller instead. Confused I have a big day in front of me...

Thanks for lesson. Smile


If you want to use CI resources inside a function, just use the reference as GSV Sleeper Service mentioned.

function foo()
    $CI =& get_instance();
    // Now you can use $CI->uri->segment() if you like
    if($CI->uri->segment(3)== ...)

Thanks Cahva. Hoping that I'm now doing something badly structured, I'm using it. Smile

"You can always change it later" - one of my oft-repeated phrases Smile

Typically its best to load an array with the data you want your view to access, pass in the data to the view when you load it, and that makes things cleaner all the way around...but it's not mandatory, no one will slap you with a ruler for doing it.

...well, they might, but you'll live.

Lol. Yes... it's a late lol. But now I need to build a menu, however, the menu subitems should appear, only when a specific uri is in place.

The menu is build on a view, since I will only use one controller for that menu generation. However, even if I put this menu generation in a helper, I was still unable to use $this->uri right?

So... I guess my ruler punishment will be a guarantee. Smile


Theme © iAndrew 2016 - Forum software by © MyBB