1

I'm trying every variation of the following to refer to a static property:

get_called_class()::$$prop

I've tried this:

${get_called_class()}::$$prop

I've tried many things, but can't seem to get it.

I know I can just do this:

$className = get_called_class();
$className::$$prop

BUT, that means an extra line of code. Surely there must be a way for the language to make this work on the same line. Anyone have a solution?

(By the way, the static property is protected, so it fails with ReflectionClass::getStaticPropertyValue.)

OCDev
  • 2,280
  • 3
  • 26
  • 37
  • Did you try `(get_called_class())::$$prop` –  Dec 31 '13 at 19:17
  • 5
    Please don't do this. Also, I would recommend burning the machine, which contains this code, before it lays eggs. – tereško Dec 31 '13 at 19:19
  • 1
    If something is so fundamentally hard, maybe you shouldn't be doing it in the first place. – deceze Dec 31 '13 at 19:21
  • @tereško: Why the assumption about the surrounding code? Framework level implementations often stretch a language. Consider it was the frameworks that have driven the demand for so many improvements in recent years to accommodate what used to require ugly hacks. I'm doing something clever here that you can't see. Promise! – OCDev Jan 01 '14 at 02:10
  • @deceze: Fundamentally hard? That's like taking aim at the premise of this entire website. :D – OCDev Jan 01 '14 at 02:11
  • @tereško: I failed to mention, the example above shown with ::$$prop isn't even in the original code; this was heavily modified for the example. So again, premature judgements like that aren't helpful. Thanks for trying though :) – OCDev Jan 01 '14 at 02:22
  • @tereško: Wait. It all makes sense now. You're being amusingly condescending at other people's expense in order to get votes and rack up points. Tisk tisk. – OCDev Jan 01 '14 at 02:28
  • @FriendlyDev are you aware, that upvotes of comments do not produce any points. – tereško Jan 01 '14 at 03:01
  • 1
    @FriendlyDev, our dear friend teresko has a (well-founded) dislike for most things static. Usually static things are a [code smell](http://en.wikipedia.org/wiki/Code_smell) that you should be careful to avoid unless there are better (PHP-centric) ways. Without any context to go on, the code as given is *scary bad* with regard to conceptual not-shooting-one's-self-in-the-foot-ness. – Charles Jan 01 '14 at 03:22
  • @Charles: Fair enough. I agree with both of you and don't like things static either. (This is why I thanked the official answer below for the "reminder" because I don't use it much.) But when building a framework that must take static things into account in the event it is used somewhere, sometimes the "static" keyword is needed, which is what I was looking for here. I am not going to show you my original code, but I assure you that what was given here strongly misrepresents the idea and I suspect you wouldn't object as strongly, if at all, if you did see it. – OCDev Jan 01 '14 at 14:56
  • @tereško: I did not know this. I wholeheartedly retract my accusation. Your comment was rather incendiary in more than one way. :) – OCDev Jan 01 '14 at 15:00
  • @Charles: Also, thank you for the link on code smell. That is good terminology and I will keep that in mind when I encounter it. – OCDev Jan 01 '14 at 15:04

1 Answers1

2

Without understanding any additional context here, you don't need to actually invoke get_called_class to poke at LSB-resolved static properties. Instead, use the static keyword to automagically resolve the currently called static class name.

class A {

    static $foo = 'from a';

    public static function test($property) {
        echo static::$$property, "\n";
    }

}

class B extends A { static $foo = 'from b'; }
class C extends A { static $foo = 'from c'; }

Example from the PHP interactive prompt:

php > include '/tmp/get_called_class.php';
php > A::test('foo');
from a
php > B::test('foo');
from b
php > C::test('foo');
from c
php > 
Charles
  • 50,943
  • 13
  • 104
  • 142