CodeIgniter Forums
Newbie: Fatal error: Using $this when not in object context - URI Helper. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Newbie: Fatal error: Using $this when not in object context - URI Helper. (/thread-22393.html)



Newbie: Fatal error: Using $this when not in object context - URI Helper. - El Forum - 09-08-2009

[eluser]MEM[/eluser]
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:
Code:
if($this->uri->segment(3)==$arrMenu[i][$strChildKey]['id_cat'])
{


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?

Márcio


Newbie: Fatal error: Using $this when not in object context - URI Helper. - El Forum - 09-08-2009

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

Márcio


Newbie: Fatal error: Using $this when not in object context - URI Helper. - El Forum - 09-08-2009

[eluser]GSV Sleeper Service[/eluser]
because vars within functions are not global, unless you use the 'global' keyword.
eg
Code:
$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.


Newbie: Fatal error: Using $this when not in object context - URI Helper. - El Forum - 09-08-2009

[eluser]MEM[/eluser]
Thanks. In fact CI allows us to do this by it's own methods right?
http://ellislab.com/codeigniter/user-guide/general/controllers.html#passinguri

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

Márcio


Newbie: Fatal error: Using $this when not in object context - URI Helper. - El Forum - 09-08-2009

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

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



Newbie: Fatal error: Using $this when not in object context - URI Helper. - El Forum - 09-09-2009

[eluser]MEM[/eluser]
Thanks Cahva. Hoping that I'm now doing something badly structured, I'm using it. Smile


Newbie: Fatal error: Using $this when not in object context - URI Helper. - El Forum - 09-09-2009

[eluser]BrianDHall[/eluser]
"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.


Newbie: Fatal error: Using $this when not in object context - URI Helper. - El Forum - 10-27-2009

[eluser]MEM[/eluser]
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


Regards,
Márcio