Maybe my question has been asked several times, but...
I have the following code
abstract class ParentClass
{
protected static $count=0;
public static function inc()
{
static::$count++;
}
public static function getCount()
{
return static::$count;
}
}
class FirstChild extends ParentClass
{
}
class SecondChild extends ParentClass
{
}
And I use it just like this
FirstChild::inc();
echo SecondChild::getCount();
It shows me "1". And as you probably guess I need "0" :)
I see two ways:
- Adding
protected static $count=0;
to each derivative classes Make
$count
not integer but array. And do sort of such things ininc
andgetCount
methods:static::$count[get_called_class()]++;
and
return static::$count[get_called_class()];
But I think these ways are a bit ugly. First - makes me copy/paste, what I'd like to avoid. Second - well, I don't know:) I just don't like it.
Is there a better way to achive what I want?
thanks in advance.