11t1 Posted February 15, 2007 Share Posted February 15, 2007 Hi, I've just installed a new PHP/MySQL development server on my local machine, and now a few of the sites I've developed are starting to act really screwy. For the most part, I'm just getting this error on every page: Fatal error: Using $this when not in object context in C:\....\head.inc on line 64 Line 64 of head.inc goes a little something like this: $this->selfurl = $_SERVER["PHP_SELF"]; Basically, I've been using the url of each page to determine what each page will look like. For instance, if you're in the news area, the 'news' nav item will display as selected while other ones won't: switch (true) { case (preg_match("/\/news\//i", "$this->selfurl")) : $navclass_news = "headernavsel"; break;... etc. The script runs no problem on the 3 sites running it, but not on my localhost. I thought it might be the register_globals variable, but when I switched to 'on' & restarted I still had the same problem. I then sifted through the phpinfo() line by line comparing my localhost to one of the live servers and couldn't find any obvious differences. Do any of you fine people know what my problem might be? Link to comment https://forums.phpfreaks.com/topic/38617-using-this-when-not-in-object-context/ Share on other sites More sharing options...
JasonLewis Posted February 15, 2007 Share Posted February 15, 2007 well that means your using $this outside of a class. make sure that your inside of a class. Link to comment https://forums.phpfreaks.com/topic/38617-using-this-when-not-in-object-context/#findComment-185330 Share on other sites More sharing options...
Jenk Posted February 15, 2007 Share Posted February 15, 2007 well that means your using $this outside of a class. make sure that your inside of a class. Being pedantic, but it means outside of an object. <?php class Foo { public static fooBar () { $this->foobaroo = 1; } } Foo::fooBar(); // fatal error: $this outside of object context. ?> Link to comment https://forums.phpfreaks.com/topic/38617-using-this-when-not-in-object-context/#findComment-185332 Share on other sites More sharing options...
11t1 Posted February 15, 2007 Author Share Posted February 15, 2007 OK, I'll see what I can do to incorporate it within an object. That said, does anyone know why it works on one server (PHP 4.1.2) and not on another (PHP 5.2.0)? Link to comment https://forums.phpfreaks.com/topic/38617-using-this-when-not-in-object-context/#findComment-185362 Share on other sites More sharing options...
o3d Posted February 15, 2007 Share Posted February 15, 2007 I assume it is bacause php 5 has stricter (more propper) class handling Link to comment https://forums.phpfreaks.com/topic/38617-using-this-when-not-in-object-context/#findComment-185366 Share on other sites More sharing options...
Jenk Posted February 15, 2007 Share Posted February 15, 2007 Possibly. Without the code on our screen we couldn't say the exact cause. Link to comment https://forums.phpfreaks.com/topic/38617-using-this-when-not-in-object-context/#findComment-185381 Share on other sites More sharing options...
11t1 Posted February 15, 2007 Author Share Posted February 15, 2007 Here's the offending code in its entirety: <? $this->selfurl = $_SERVER["PHP_SELF"]; $navclass_home = $navclass_news = $navclass_bio = $navclass_discog = $navclass_pics = $navclass_timeline = $navclass_slog = $navclass_words = $navclass_tutorials = $navclass_wishlist = $navclass_links = $navclass_contact = $navclass_subscribe= $navclass_faq = "headernav"; switch (true) { case (preg_match("/\/news\//i", "$this->selfurl")) : $navclass_news = "headernavsel"; break; case (preg_match("/\/discog\//i", "$this->selfurl")) : $navclass_discog = "headernavsel"; break; case (preg_match("/\/pics\//i", "$this->selfurl")) : $navclass_pics = "headernavsel"; break; case (preg_match("/\/bio\//i", "$this->selfurl")) : $navclass_bio = "headernavsel"; break; case (preg_match("/\/timeline\//i", "$this->selfurl")) : $navclass_timeline = "headernavsel"; break; case (preg_match("/\/slog\//i", "$this->selfurl")) : $navclass_slog = "headernavsel"; break; case (preg_match("/\/words\//i", "$this->selfurl")) : $navclass_words = "headernavsel"; break; case (preg_match("/\/tutorials\//i", "$this->selfurl")) : $navclass_tutorials = "headernavsel"; break; case (preg_match("/\/wishlist\//i", "$this->selfurl")) : $navclass_wishlist = "headernavsel"; break; case (preg_match("/\/links\//i", "$this->selfurl")) : $navclass_links = "headernavsel"; break; case (preg_match("/\/contact\//i", "$this->selfurl")) : $navclass_contact = "headernavsel"; break; case (preg_match("/\/subscribe\//i", "$this->selfurl")) : $navclass_subscribe = "headernavsel"; break; case (preg_match("/\/faq\//i", "$this->selfurl")) : $navclass_faq = "headernavsel"; break; default: $navclass_home = "headernavsel"; break; } ?> "headernav" & "headernavsel" are just css class names to determine the style of each nav item: <a href="/news/" class="<? echo($navclass_news); ?>">news</a> thx Link to comment https://forums.phpfreaks.com/topic/38617-using-this-when-not-in-object-context/#findComment-185405 Share on other sites More sharing options...
Jenk Posted February 15, 2007 Share Posted February 15, 2007 that line ($this->etc.) is completely out of place. Link to comment https://forums.phpfreaks.com/topic/38617-using-this-when-not-in-object-context/#findComment-185411 Share on other sites More sharing options...
11t1 Posted February 15, 2007 Author Share Posted February 15, 2007 guess i just got lucky ok, i'm going back to the drawing board to figure out a legal way to get what i want thx Link to comment https://forums.phpfreaks.com/topic/38617-using-this-when-not-in-object-context/#findComment-185418 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.