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 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: Andrew
Sent on: Thursday, March 26, 2009 9:02 PM
'this' is used when referring to the current object. It's an objects
way of saying "I'm gonna use my version". self is used to refer to a
class' value for something. This is useful for things that are used
that are independent of which instance is referring to it.

<?php
// filename: person.php

class Person {
    private $name = '';

    function setName($firstname){­
         $this->name = $firstname;
    }

    function getName(){
         return $this->name;
    }

    function say_my_name(){
        echo "hello " . $this->getName();­
    }
}

?>
lets say there's are 2 functions called say_my_name() defined - 1 in
your main.php, 1 in person.php

<?php


include('person.php'­);

function say_my_name(){
     echo "hello world";
}

say_my_name(); // displays 'hello world'

$awoods = new Person();
$awoods->setName(­'Andrew');
$awoods->say_my_n­ame(); // displays 'Hello Andrew'

$obama = new Person();
$obama->say_my_na­me(); // displays 'Hello '

?>

Each object above is using it's own version of the value to display.
hope that helps.




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




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

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