addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcredit-cardcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1launch-new-window--smalllight-bulblinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Re: [php-49] OOP novice: this vs. self

From: Nick J.
Sent on: Friday, March 27, 2009 11:41 AM
David

exampleClass::$foo does not instantiate or create anything. It merely references the static property $foo of the class exampleClass.

Yes, you can think of it as a superglobal.?

When referencing $foo from outside the exampleClass, use exampleClass::$foo
When referencing $foo from within a function in exampleClass, use self::$foo

Note that $foo is not associated with any specific instance (or object) of exampleClass. It is only associated with the class definition itself. So I'd say Ryan's statement "It does not, however, change the value of $foo within any of your instances of the exampleClass." is a little misleading.?

---
www.diffen.com
Diffen. Discern. Decide.


On Fri, Mar 27, 2009 at 10:36 AM, David Malouf <[address removed]> wrote:
Nick,

Thank you for this email and the time you took to make that wiki page.

And thank you to all of you for the insights - including Darcy's "don't bother" (because Darcy seems to know what I'm doing and is 100% correct in his assessment that I am FAR from ever needing static/'self' ).

Nick, one question about the wiki example: when you coded:
?? exampleClass::$foo = "Hello";

does that instantiate an 'exampleClass' or is exampleClass:$foo created (almost like a superglobal)?


Thanks again to all - I am very confident that I can ignore 'self' (I mean that in a coding way, not a philosophical/nihilism way!!),
David


--- On Thu, 3/26/09, Nick Jasuja <[address removed]> wrote:
From: Nick Jasuja <[address removed]>

Subject: Re: [php-49] OOP novice: this vs. self
To: [address removed]
Date: Thursday, March 26, 2009, 11:26 PM

I figured a lot of people probably have the same question. So I wrote 2 wiki articles to explain with? (hopefully simple) examples:
this vs. self - http://www.diffen.com/difference/Self_(PHP)_vs_this_(PHP)
class vs. object - http://www.diffen.com/difference/Class_vs_Object
---
www.diffen.com
Diffen. Discern. Decide.


On Thu, Mar 26, 2009 at 11:09 PM, Ryan Biesemeyer <[address removed]> wrote:
Self refers to the current class, $this refers to the current object. The beauty of $this is that you can have many objects of the same class that are each self-aware, meaning they operate on themselves independent of the other instances of the same class. Self is often used to reference static methods and variables, but it can also be used in other ways. Consider this pseudo code:?

class Foo {
??public static function load() {
?? ?if(!isset(self::$instance)){
?? ? ?self::$instance = new self;
?? ?}
?? ?return self::$instance;
??}

??protected function __construct() {
?? ?$this->bar=microtome(true);
??}
}

// check to see that the static bar in foo is empty
var_dump(Foo::bar);

// use load to get fiddle
$fiddle= Foo::load();

//get bar, which was set to the current micro time
var_dump($fiddle->bar);

//sleep to make the microtome different
sleep(1);

// load cow with our instance.?
$cow = Foo::load();

// returns the same object as before to a different handle. If this were not the case, bar would be different because of the sleep.
var_dump($cow->bar);

// static bar is still not set
var_dump(Foo::bar);

Probably raises more questions, but I hope it helps.?

-Ryan

Sent from my iPhone

On Mar 26, 2009, at 10:36 PM, "darcy w. christ" <[address removed]> wrote:

not exactly. ?static is not just methods/properties declared static, but rather the way in which the class is used. ?most classes are instantiated (ie. a unique object is created of the class type). ?i would suggest to you that you will generally only use classes this way, at least in the beginning. ?thus, in your example, use $this. ?self allows for a way to reference the class when it is called statically (ie. not instantiated). ?

i never really use this (i mean self ;-) and generally either don't understand when/how to use this, or philosophically don't agree with this aspect of OOP.

so my 2 cents for you, david, is to use $this. ?forget about self until you find yourself calling a class statically (ie.?PrimaryClass::getContent()) and find yourself wondering how you reference the class (not object, since there are no objects in that context).




On Mar 26, 2009, at 9:59 PM, David Malouf wrote:

So 'self' is ONLY used with methods/properties that are declared 'static' - is that correct?

Here's where the class vs. object distinction confuses me - does 'calling' another method in a class mean I'm calling it in the Object or the Class.? Here's a completely worthless example that might make my confusion easier to see (no pun intended).? [I know this is a mal-formed class, I'm just trying to highlight my confusion]

class PrimaryClass
{
??? public function getData ($theURL = false) {
???????? if ($theURL) {

??????????????? // do I use 'this' as in . . .
??????????? $theData = $this->getContents;
??????????????? // or do I use 'self' as in . . .
??????????? $theData = self::getContents;

??????????? return $theData;
???????? } else {
??????????? return "Give me a URL silly!";
???????? }
??? }
??? public function getContents($url) {
?????????? file_get_contents($url,'',$contentsAreInHere);
?????????? return $contentsAreInHere;
??? }
}

Is my lack of understanding any clearer?!?

(And thank you, Andrew, for the distinctives of 'this' with multiple classes + methods -- that answered an issue I was going to come up against [as I look at my project]!!)

David




--- On Thu, 3/26/09, Joel Simpson <[address removed]> wrote:
From: Joel Simpson <[address removed]>
Subject: Re: [php-49] OOP novice: this vs. self
To: [address removed]
Date: Thursday, March 26, 2009, 8:09 PM

following are posts taken from? http://stackoverflow.com/questions/151969/php-self-vs-this

Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.?
--- John Millikin

"self" (not $self) refers to the type of class, where as $this refers to the current instance of the class. "self" is for use in static member functions to allow you to access static member variables. $this is used in non-static member functions, and is a reference to the instance of the class on which the member function was called.

Because "this" is an object, you use it like: $this->member Because "self" is not an object, it's basically a type that automatically refers to the current class, you use it like: self::member

--- MrZebra


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joel R. Simpson ?| ?[masked] (cell/text/page)
AIM/meebo/Yahoo: joelrsimpson ?| ?GoogleChat: [address removed] ?| ?MSN: [address removed]
http://www.linkedin.com/in/joelsimpson | http://my.significantbits.com/JoelSimpson ?


On Thu, Mar 26, 2009 at 7:47 PM, David Malouf <[address removed]> wrote:
Still trying to wrestle my way in to OOP...

I cannot seem to figure out the different uses for 'this' vs 'self'.? I'm pretty sure it's a simple-ish distinction, but I cannot wrap my mind around it yet.

Can someone explain this distinction - I'm hoping that reading it in a way I haven't read before will be what it takes for me to 'get it'!


Sorry for such a newbie question,
David Malouf





--

Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by David Malouf ([address removed]) from The Seattle PHP Meetup Group.
To learn more about David Malouf, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway, New York, NY 10012 USA





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Joel Simpson ([address removed]) from The Seattle PHP Meetup Group.
To learn more about Joel Simpson, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway, New York, NY 10012 USA





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by David Malouf ([address removed]) from The Seattle PHP Meetup Group.
To learn more about David Malouf, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway, New York, NY 10012 USA


~/darcy w. christ
1000camels






--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by darcy w. christ ([address removed]) from The Seattle PHP Meetup Group.
To learn more about darcy w. christ, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway, New York, NY 10012 USA




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Ryan Biesemeyer ([address removed]) from The Seattle PHP Meetup Group.
To learn more about Ryan Biesemeyer, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway, New York, NY 10012 USA





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Nick Jasuja ([address removed]) from The Seattle PHP Meetup Group.
To learn more about Nick Jasuja, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway, New York, NY 10012 USA





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by David Malouf ([address removed]) from The Seattle PHP Meetup Group.
To learn more about David Malouf, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway, New York, NY 10012 USA

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy