Sunday 12 October 2014

Re: [dcphp-dev] PHP self:: vs static::

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?



On Sun, Oct 12, 2014 at 4:32 PM, Ray <ray.paseur@gmail.com> wrote:
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

--
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-dcphp-group@googlegroups.com
To unsubscribe, send email to washington-dcphp-group+unsubscribe@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-group+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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-dcphp-group@googlegroups.com
To unsubscribe, send email to washington-dcphp-group+unsubscribe@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-group+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

0 comments:

Post a Comment