Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pupose of $this

Status
Not open for further replies.

wolf73

Programmer
Feb 12, 2006
93
CA
class myclass
{
var $hope;

function meyou()
{
$this->hope="future";
}

function printme()
{
echo "$this->hope";
}


}

Will it be true that the reason we use $this, is to make variable or object at class level so It can be accessed by any funtion with out having to pass it to that function as an argument?
 
In OOP Terminology $this refers to the current object.

Code:
 function meyou()
   {
      $this->hope="future";
   }
The above piece of code assigns variable '$hope' of class 'myclass' a value 'future'.
So inside class 'myclass', the class can be referred to as '$this'.




--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Thanks. Now will it be wrong if we do the following with out $this

class myclass
{
public $hope;

function meyou()
{
$hope="future";
}

function printme()
{
echo "$hope";
}


}

 
Wrong? I do not know. What are you trying to do?

Are you trying to assign a value to variable in the method-level variable scope? A value that will not be there the next time the method is run? Then your code will do admirably.

Are you trying to assign a value to the class-level variable-scoped variable you created with the line "public $hope;"? A value that will exists for the life of the object instantiated form the class definition? If so, you have to use "$this->hope".

Perhaps another example is in order.

Code:
<?php

class foo
{
	protected $bar;			//create a class-scoped variable
	
	function function1 ()
	{
		$bar = 3;			//assign a value to a method-level variable
		$this->bar = 4;		//assign a value to a class-level variable
		
		print $bar . '<br>';
		print $this->bar . '<br>';
	}
	
	function function2 ()
	{
		print $bar;			//output value from a method-level variable
		print $this->bar;	//output value from a class-level variable
	}
}

$a = new foo();

$a->function1();
$a->function2();
?>

NOTE: I am doing a very unwise thing in my code, specifically using the same variable name at the method-level scope and the class-level scope. I am doing this strictly for the purposes of pedagogy. Under no circumstances should one do this in production code.

The output of this script on my system is:

3
4

Notice: Undefined variable: bar in /home/sites/test/html/test_class.php on line 18
4


It outputs the value of the method-scope variable then the class-scope variable.

Then it outputs an error for the non-existence of the method-scope variable that only existed for the time the function1() method ran. After that, the variable no longer existed because its scope no longer existed.

Then it outputs the existing class-scoped variable.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks for your example. I did C++ sometime back. I guess things are little different there. If I remmeber correct once you declare variable supose scope public. Then it stays public. You can not change its scope when you assign value to it.

DO you know when php 5 came out?
 
Thanks for your detailed example. I did C++ quite sometime back. I think was geting little confused bcz of that. There if remmeber right, once you declare a variable suppose public. Then it stay public all the way. You can not change the scope the moment you assign value to it...

Do you know when did php 5 came out?
 
Do not confuse "public", "private" and "protected" with a variable's scope. The two have nothing whatsoever to do with each other.

The adjectives "public", "private" and "protected" describe a variable's visibility, and detail what types of code can access a particular class-level-scoped variable. In fact, the visibility adjectives can't even be used with method-level-scoped variables, only with class-level-scoped variables.



According to , PHP version 5.0.0 was released 20040713.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top