4 Replies - 4196 Views - Last Post: 23 January 2010 - 04:16 PM Rate Topic: -----

#1 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Using $this when not in object context

Posted 23 January 2010 - 01:30 PM

On a small project that i am working on i am loading files and classes depending on the url and everything works fine except that in my error log i keep getting the following:

Quote

[error] [client 127.0.0.1] PHP Fatal error: Using $this when not in object context in C:\\wamp\\www\\system\\engines\\system.php on line 133


This is line 133 of system.php:
$this->redirect($this->config['site']['base_url'], 301, true);



Here is the entire function
	public function loadModule($module)
	{
		if(!is_string($module))
		{
			trigger_error("system::loadModule: This function only supports single loads at a time", FATAL);
		}

		if(include_once("system/modules/{$module}.php"))
		{
			return new $module;
		} else {
			trigger_error("system::loadModule: Unable to find the module '{$module}' within the modules folder... Redirecting to 'base url'", WARNING);

			$this->redirect($this->config['site']['base_url'], 301, true);
		}
	}



This is the code that calls the function (this is index.php)
if($_SERVER['REQUEST_URI'] == '/' || $_SERVER['REQUEST_URI'] == '/index.php')
{
	$control[1] = $config['site']['default'];
} else {
	$control = explode("/", $_SERVER['REQUEST_URI']);
}


$controller = $system::loadModule($control[1]);



Now after googling the error message all i can really find is stuff to do with code migration from php 4 to php 5. Currently i am running php 5.3.0 so any help would be very appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: Using $this when not in object context

#2 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Re: Using $this when not in object context

Posted 23 January 2010 - 01:30 PM

A double thread post turned into a double post.... weird

This post has been edited by Auzzie: 23 January 2010 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Using $this when not in object context

Posted 23 January 2010 - 01:39 PM

I saw it and merged them.
Was This Post Helpful? 0
  • +
  • -

#4 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Using $this when not in object context

Posted 23 January 2010 - 04:08 PM

Your error is on this line in your index.php:
$controller = $system::loadModule($control[1]);

The double colon indicates a static method call. You can't use $this in a static context because it doesn't exist (by definition, $this is the current object, and static methods aren't called on objects). Although PHP will let you call methods statically without explicitly declaring them as static, you still can't reference $this when calling an instance method in a static context.
Was This Post Helpful? 1
  • +
  • -

#5 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Re: Using $this when not in object context

Posted 23 January 2010 - 04:16 PM

Thanks Adahacker, that section of code i just randomly ported over and never thought to change, silly me
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1