Jump to content

Using $this when not in object context...


11t1

Recommended Posts

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

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.

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.