tags:

views:

23

answers:

2

I have a problem with static keyword due to inheritance in PHP 5.3.

abstract class Object
{
    protected static $_classDataSource = null;

    public static function getDataSource()
    {
        return static::$_classDataSource;
    }

    public static function setDataSource( $dataSource)
    {
        static::$_classDataSource = $dataSource;
    }
}

class Film extends Object
{

}

class Actor extends Object
{

}
Film::setDataSource('FFF');
Actor::setDataSource('aaa');
echo Film::getDataSource();
echo Actor::getDataSource();

Result is: aaaaaa Expected result: FFFaaa

What should I do to make it as expected?

+2  A: 

You need to redeclare the static variables in the child classes or break the reference set manually. See this answer.

Artefacto
A: 

I know it's not technically answering your exact question, but I have to ask: Why? If you need configuration (such as setting a data source), in most cases it's better to use instances...

ircmaxell