TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-13-2009, 04:01 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default The "$this->" variable...

Ok so I've been reading a million books to fully understand how $this-> works and I think I've got it. I'll try to explain, I hope I get it right, for the sake of brevity, we have a simple class here:

PHP Code:
class myClass {

public 
$variable1;

public function 
method() {

}

}
// END CLASS 

When the main script instantiates this class and creates the object:

$myClass = new myClass();


Well, $myClass-> and $this-> are basically the same thing. You can access methods/properties the same way.

The only difference is that if a property/method is set to private, then the outside object will have no direct access, but the inside object "$this->" will have access....

And that's pretty much it? Did I get it right?

Someone? Anyone??

Are there any other differences between the two that I missed?
allworknoplay is offline  
Reply With Quote
Old 05-13-2009, 05:38 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

You've got it somewhat right, but not fully. $this only refers to the current instance of an object created from a class. So, given the following simple class:

Code:
class SimpleClass
{
    public $var1 = 'my first variable';
}

$object1 = new SimpleClass();
echo $object1->var1; // outputs 'my first variable'

$object2 = new SimpleClass();
$object2->var1 = 'the second one';
echo $object2->var1; // outputs 'the second one'

echo $object1->var1; // still outputs 'my first variable'
Note that the code above is not optimized, it's only used for demonstration purposes.

Now, you can see that we transform the $var1 public member (that's the name of class variables). In each case, we have a 'separate' $this variable at the class level (inside it), that is tied to each object you create from that class. $this is a special variable referring to "the current object". And that's really what it is...so you don't have to create instances of the class you're writing inside the very same class. Instead, you're only programming a prototype of an object, customizing each instance as you need. $this, however, is not available outside a class (as you've probably already seen), because then it would loose its meaning.

I hope I've been clear enough. If not, feel free to ask questions, and you will be answered Keep up the good work
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
allworknoplay (05-13-2009)
Old 05-13-2009, 05:55 PM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
You've got it somewhat right, but not fully. $this only refers to the current instance of an object created from a class. So, given the following simple class:

Code:
class SimpleClass
{
    public $var1 = 'my first variable';
}

$object1 = new SimpleClass();
echo $object1->var1; // outputs 'my first variable'

$object2 = new SimpleClass();
$object2->var1 = 'the second one';
echo $object2->var1; // outputs 'the second one'

echo $object1->var1; // still outputs 'my first variable'
Note that the code above is not optimized, it's only used for demonstration purposes.

Now, you can see that we transform the $var1 public member (that's the name of class variables). In each case, we have a 'separate' $this variable at the class level (inside it), that is tied to each object you create from that class. $this is a special variable referring to "the current object". And that's really what it is...so you don't have to create instances of the class you're writing inside the very same class. Instead, you're only programming a prototype of an object, customizing each instance as you need. $this, however, is not available outside a class (as you've probably already seen), because then it would loose its meaning.

I hope I've been clear enough. If not, feel free to ask questions, and you will be answered Keep up the good work

Thanks Xenon,

Makes perfect sense. I had not even thought about that. At my level, I'm only thinking about one instance of a specific class, but it's good to remember that $this-> refers to it's own instance. I suppose you can call it an "instance" property of the class, the same way regular properties and methods are considered.

I'd hate to see how confused I get when having to deal with multiple instances of the same class...I hope that's not something that happens too often and only used in books and examples...
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tip] Variable Prefixes bluesaga Tips & Tricks 24 10-22-2012 09:40 AM
how would i echo this javascript variable sarmenhb Javascript, AJAX, E4X 1 12-03-2008 02:06 PM
What is a Variable? codefreek Absolute Beginners 7 07-01-2008 08:29 PM
Calculating an unknown variable ReSpawN General 6 05-20-2008 04:47 PM
Easy Peasy Variable Validation quantumkangaroo Absolute Beginners 3 03-20-2008 03:30 PM


All times are GMT. The time now is 09:06 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design