use of $this outside of class definition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdhiren
Forum Newbie
Posts: 2
Joined: Mon Mar 30, 2009 11:03 pm

use of $this outside of class definition

Post by cdhiren »

does anyone know usage of "$this->" outside of a class definition.

e.g: main.php

<?php

switch($this->mSubModule)

?>

without creating any object, what could be the significance of above code? i want to understand the flow.

i have the above piece of code written by some other developer but could figure out how is it possible to access class members without instantiating the object

thanks in advance.

regards

dhiren
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: use of $this outside of class definition

Post by requinix »

cdhiren wrote:i have the above piece of code written by some other developer but could figure out how is it possible to access class members without instantiating the object
Well, it's not possible. You can't access something that doesn't exist.

That code does not make sense unless it is used inside of a class function. In fact, besides not making sense, it's not even possible: even attempt to name a variable "this" and you'll get an "not in object context" or "cannot reassign" error.
cdhiren
Forum Newbie
Posts: 2
Joined: Mon Mar 30, 2009 11:03 pm

Re: use of $this outside of class definition

Post by cdhiren »

thanks avatar. thats exactly what even i thought off. but somehow this piece of code is working. can i have your email so that i could provide the source details ...

thanks
dhiren
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: use of $this outside of class definition

Post by Benjamin »

cdhiren wrote:somehow this piece of code is working. can i have your email so that i could provide the source details ...
It's not working. Also, we really don't provide private support here. Feel free to post your code, using the approriate code tags.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: use of $this outside of class definition

Post by Chris Corbyn »

cdhiren wrote:thanks avatar. thats exactly what even i thought off. but somehow this piece of code is working. can i have your email so that i could provide the source details ...

thanks
dhiren
Is this inside a template by any chance? Any file that is included by a class has access to $this:

MyClass.php

Code: Select all

<?php
 
class MyClass {
  public $myVar;
  
  public function setMyVar($value) {
    $this->myVar = $value;
  }
  
  public function dump() {
    include 'other-file.php';
  }
}
other-file.php

Code: Select all

<?php
 
printf("\$this->myVar = %s<br />\n", $this->myVar);
 
So if you were to create an instance of MyClass and call setMyVar() and then dump() you'd be giving the included file access to $this:

Code: Select all

<?php
 
$m = new MyClass();
$m->setMyVar(42);
$m->dump();
// $this->myVar = 42
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: use of $this outside of class definition

Post by Benjamin »

Just to be clear, $this references the class that it's inside of. You cannot use $this outside of any class, period.
Post Reply