Scope of $this in php

class Planet {
public $World;
public function __construct() {}// so it can be used anytime: it also allows you to create a new object
$this->World = "Hello world!";
Public function setWorld($val)
{
If($val != NULL) $this->World = $val;
Else{die(“Earth must contain a string”);}
}
}
$newPlanet = new Planet();
$newPlanet->setWorld($_GET[“Planet”]);
echo $newPlanet->World;

So, on 4th line , it has declared $this->World = "Hello world!"; outside the scope of the function. So when i saved the same code on my localhost then it is giving me an error which is it must be declared inside the function. Till now, i also have an idea that we can declare $this inside the function and it states the current object. So i just want to know that can we declare $this outside of the function, if yes then how shall we do it?

Re: Scope of $this in php

It would be better if you will tell me the answer instead of leaving a msg that won't help me...

The purpose of my question was to avoid the drivel on lines 10 & 12.

I understand that my reply wasn't to the exact question, thus why I stated 'off topic'

phplearner_null, on 27 December 2012 - 11:30 AM, said:

Any help would be appreciated much

I would suggest you learn proper code writing before spending time on this. The code indentation is bad, there are weird spaces all over, you're going to create more hurdles in the future without proper fundamentals than it's worth. Just my suggestion, & I understand it won't be taken well. But there you have it.

Re: Scope of $this in php

Posted 27 December 2012 - 09:45 AM

Totally on topic:

You cannot use $this outside of a function within a class. See the documentation: PHP OOP Basics

I would recommend initializing default values within the __construct () function which is what I believe what you are trying to do. If not, then you should be able to accomplish anything else with your internal variables within the functions in the class.

Cheers.

This post has been edited by Dormilich: 27 December 2012 - 10:03 AM
Reason for edit:: fixed link

Re: Scope of $this in php

It would be better if you will tell me the answer instead of leaving a msg that won't help me...

The purpose of my question was to avoid the drivel on lines 10 & 12.

The purpose of my question is to know more and clear my concepts about oops. To be honest, i really suck now in oops or you can say that the questions which i have been asked on this forum is easy for you that's why you have called it an off topic but for me it's the beginning era of mine on oops concept.
I know one day i will also do coding like others which i cannot do right now. Thanks to you and have a nice day..

trichardson, on 27 December 2012 - 09:45 AM, said:

Totally on topic:

You cannot use $this outside of a function within a class. See the documentation: <a href="http://php.net/manual/en/language.oop5.basic.php">PHP OOP Basics</a>

I would recommend initializing default values within the __construct () function which is what I believe what you are trying to do. If not, then you should be able to accomplish anything else with your internal variables within the functions in the class.

Cheers.

Thanks trichardson mate, I really appreciate that you gave me the time for clarifying my concepts about oops. That's what i wanted to know about it.. Really thanks a lot..

Re: Scope of $this in php

$this has no meaning outside of a member function, as there is no instance that it can refer to. I assume you may have intended:

public function __construct() {
$this->World = "Hello world!";
} // so it can be used anytime: it also allows you to create a new object

It's almost like, if there was proper code indentation, one might have found that problem on their own?

So.... perhaps I was right?!

Indeed, you are right. My question was that:- I was reading about the topic named "5 reasons why we use oops". So in that article, i have seen a code which is below:
the code !
I just wanted to confirm that can we declare $this outside the function or not..
You and other peoples helped me for getting the answer of my question.. Thanks alot..

Re: Scope of $this in php

Posted 27 December 2012 - 10:02 AM

There are lots of bad tutorials on the web. This one isn't too terrible, but it does have mistakes. The close brace for the constructor needs to be moved, and the indenting is poor. Here's how I'd do it: