let us say there is Class A. In the constructor of this class I create an Object B of Class B.
Now what is the difference between these two ?
this->B = new B;
and
B = new B;
The "this" object always points to the current instance of an object. So whenever you refer to a variable with $this->, you are accessing the variable that is in the current object's memory space. Here is an example :
class a {
var $foo; var $bar;
function a() { $this->foo = "Hello "; $this->bar = "World"; $foo = "Widgets"; } function dump() { echo $this->foo.$this->bar; } }
$obj = new a(); $obj->dump();
This above snippet will print "Hello World", because the $foo variable that is created in the constructor does not have object scope. In other words, $foo only exists in that function. You can verify this by using the print_r function thusly :
echo "<pre>"; print_r($obj); echo "</pre>";
To answer your original question, the difference between using $this-> and not is that with $this-> the variable referenced is one that exists in the object's scope. If you want to create an instance of the object that you don't plan to use in other members of that class, then you can make it "local" to that member by omitting $this->
-- Burhan Khalid phplist[at]meidomus[dot]com http://www.meidomus.com
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php