addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1light-bulblinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

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

From: Andrew
Sent on: Thursday, March 26, 2009 10:48 PM
Darcy raises a good point and I agree. The common scenario is to use
$this when inside a class. So it's probably best to not think about
'self' for now.

// Andrew Woods / Web Developer / http://mastodonla...­




On Thu, 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($u­rl,'',$contentsAreIn­Here);
> ��������������������­ 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://stackoverf...­
>
> 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.linked...­ |
> http://my.signifi...­
>
>
> 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

Our Sponsors

  • PluralSight

    PluralSight subscriptions for developer training

  • O'Reilly

    Disc Code: PCBW is good for 40% off print and 50% off ebooks and videos

  • JetBrains PhpStorm

    Occasional free licenses to raffle off at meetups

  • DynaTrace

    Find yourself, promote yourself, stay true to yourself.

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