Jump to content

static variables in traits


stijn0713

Recommended Posts

From the manual: "Static variables can be referred to in trait methods, but cannot be defined by the trait. Traits can, however, define static methods for the exhibiting class."

 

trait Counter {
    public function inc() {
        static $c = 0;
        $c = $c + 1;
        echo "$c\n";
    }
}

 

Static variables always have to be defined in methods if i'm right, and considering that the above example works perfectly, i don't see what would be a problem to work with static variables in traits.

 

 

So my question is:

 

What is meant with "but cannot be defined by the trait"?

 

 

Link to comment
https://forums.phpfreaks.com/topic/276654-static-variables-in-traits/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.