Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Scope of $this in php

$
0
0
Dear folks,

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:-
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?

Any help would be appreciated much & thanks you in advance..:)

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>