Scope of $this in php

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1457 Views - Last Post: 27 December 2012 - 10:26 AM Rate Topic: -----

#1 phplearner_null  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-December 12

Scope of $this in php

Posted 27 December 2012 - 09:30 AM

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..:)

Is This A Good Question/Topic? 0
  • +

Replies To: Scope of $this in php

#2 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: Scope of $this in php

Posted 27 December 2012 - 09:31 AM

Totally off topic :

Are you using Microsoft Word as your editor?

Because if you are, you shouldn't be.
Was This Post Helpful? 0
  • +
  • -

#3 phplearner_null  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-December 12

Re: Scope of $this in php

Posted 27 December 2012 - 09:35 AM

View Postno2pencil, on 27 December 2012 - 09:31 AM, said:

Totally off topic :

Are you using Microsoft Word as your editor?

Because if you are, you shouldn't be.

I'm not using Microsoft word as my editor. It would be better if you will tell me the answer instead of leaving a msg that won't help me...
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: Scope of $this in php

Posted 27 December 2012 - 09:36 AM

View Postphplearner_null, on 27 December 2012 - 11:35 AM, said:

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'

View Postphplearner_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.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2891
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: Scope of $this in php

Posted 27 December 2012 - 09:43 AM

$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

Was This Post Helpful? 4
  • +
  • -

#6 trichardson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 9
  • Joined: 02-December 12

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

Was This Post Helpful? 3
  • +
  • -

#7 phplearner_null  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-December 12

Re: Scope of $this in php

Posted 27 December 2012 - 09:47 AM

View Postno2pencil, on 27 December 2012 - 09:36 AM, said:

View Postphplearner_null, on 27 December 2012 - 11:35 AM, said:

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..

View Posttrichardson, 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..:)
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: Scope of $this in php

Posted 27 December 2012 - 09:48 AM

View Postandrewsw, on 27 December 2012 - 11:43 AM, said:

$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?!
Was This Post Helpful? 4
  • +
  • -

#9 phplearner_null  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-December 12

Re: Scope of $this in php

Posted 27 December 2012 - 09:52 AM

View Postno2pencil, on 27 December 2012 - 09:48 AM, said:

View Postandrewsw, on 27 December 2012 - 11:43 AM, said:

$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..:)
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: Scope of $this in php

Posted 27 December 2012 - 09:53 AM

omg, I would avoid that site... if that's posted as example code (which I just checked & it is).... yuck.
Was This Post Helpful? 3
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2891
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: Scope of $this in php

Posted 27 December 2012 - 09:53 AM

BTW I think that article is very poor in making the case for OOP. I'm sure there are much better articles to be Googled.

This looks much better (excuse the pun!).

This post has been edited by andrewsw: 27 December 2012 - 09:59 AM

Was This Post Helpful? 1
  • +
  • -

#12 phplearner_null  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-December 12

Re: Scope of $this in php

Posted 27 December 2012 - 09:55 AM

Thanks to all for posting their replies and helping me :))
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

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:
<?php
class Planet {
	public $World;
	
	public function __construct() {
		$this->World = "Hello world!<br>";
	}
	
	public function setWorld($val) {
		if(!empty($val)) {
			$this->World = $val;
		} else {
			die("Earth must contain a string");
		}
	}
}

$newPlanet = new Planet();
echo $newPlanet->World;
$newPlanet->setWorld($_GET["Planet"]);
echo $newPlanet->World;


Hmm, I just read a little more of that tutorial and want to revise my statement. It is terrible. I suggest you read the tutorials on this site.

This post has been edited by Dormilich: 27 December 2012 - 10:07 AM

Was This Post Helpful? 2
  • +
  • -

#14 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3637
  • View blog
  • Posts: 5,766
  • Joined: 08-June 10

Re: Scope of $this in php

Posted 27 December 2012 - 10:07 AM

That article said:

We all know that there are two types of programming languages, the Procedural Type, and the Object Oriented.

Good thing none of our functionally oriented members frequent this part of the forum...

CTphpnwb said:

I suggest you read the tutorials on this site.

Agreed. To get you started, try this one, by our own macosxnerd101.
OOPHP- The Basics of Classes and Objects
Was This Post Helpful? 3
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3404
  • View blog
  • Posts: 9,625
  • Joined: 08-June 10

Re: Scope of $this in php

Posted 27 December 2012 - 10:12 AM

I might state that even with this small example, there is still a lot to discuss and point out (e.g. public property vs. setter/getter)

View PostAtli, on 27 December 2012 - 06:07 PM, said:

That article said:

We all know that there are two types of programming languages, the Procedural Type, and the Object Oriented.

Good thing none of our functionally oriented members frequent this part of the forum...

Good thing that hardly anyone considers Java​Script a Functional Programming Language…
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2