PHP self:: vs static::

48 views
Skip to first unread message

Ray

unread,
Oct 12, 2014, 4:32:03 PM10/12/14
to washington-...@googlegroups.com
In this code snippet, I get output that says "keykey"

What is the difference between the meanings of static:: and self:: in
this or any other context. Any man page reference would be gratefully
received!

<?php
error_reporting(E_ALL);

Class Thing
{
const PRIMARY_KEY = 'key';

public function keys()
{
$x = self::PRIMARY_KEY;
$y = static::PRIMARY_KEY;
return $x . $y;
}
}

$thing = new Thing;
var_dump($thing->keys());

Thanks and regards, Ray

Oscar

unread,
Oct 13, 2014, 10:23:02 AM10/13/14
to Ray, washington-...@googlegroups.com

Inheritance is where this really shows up.

- static:: refers to the calling class, ie " references the class that was initially called at runtime. "
- self:: refers to the class that actually defined the method

About static:

http://us2.php.net/manual/en/language.oop5.static.php

In particular, see the examples here:

http://us2.php.net/manual/en/language.oop5.late-static-bindings.php

Since :: is the scope resolution operator, you can remember the distinction as "'self' limits the scope to the defining class"

John Bloch

unread,
Oct 13, 2014, 11:56:27 AM10/13/14
to Ray, DCPHP Meetup

static:: uses late static binding whereas self does not. If you were to extend Thing and redefine PRIMARY_KEY (say, to 'foobar'), running keys() on an instance of that subclass would return 'keyfoobar'.

Put another way, self:: always refers only to the class it is written in, while static:: refers to the class context in which it is run.

--
You received this message because you are subscribed to the Google
Group: "Washington, DC PHP Developers Group" - http://www.dcphp.net
To post, send email to washington-...@googlegroups.com
To unsubscribe, send email to washington-dcphp-...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/washington-dcphp-group?hl=en
---
You received this message because you are subscribed to the Google Groups "Washington, DC PHP Developers Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to washington-dcphp-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Jarrett

unread,
Oct 13, 2014, 11:56:27 AM10/13/14
to Ray, washington-...@googlegroups.com
You need to check out this page. For PHP 5.3+

If I understand correctly static:: will refer to the last called class whereas self:: always refers to self. Check out this example (based on yours):


<?php
error_reporting(E_ALL);

Class Thing
{
    const PRIMARY_KEY = 'key';

    public function keys()
    {
        $x = self::PRIMARY_KEY;
        $y = static::PRIMARY_KEY;
        return $x . $y;
    }
}

class Thing2 extends Thing
{
    const PRIMARY_KEY = 'key2';

   
}

$thing = new Thing;
var_dump($thing->keys());

$thing2 = new Thing2();
var_dump($thing2->keys());

And that outputs:
string(6) "keykey"
string(7) "keykey2"

Make sense?



Reply all
Reply to author
Forward
0 new messages