PHP traits - change value of static property in inherited class
Asked Answered
P

3

13

So, this is my trait:

trait Cacheable
{
    protected static $isCacheEnabled = false;
    protected static $cacheExpirationTime = null;

    public static function isCacheEnabled()
    {
        return static::$isCacheEnabled && Cache::isEnabled();
    }

    public static function getCacheExpirationTime()
    {
        return static::$cacheExpirationTime;
    }
}

This is the base class:

abstract class BaseClass extends SomeOtherBaseClass
{
    use Cacheable;
    ...
}

These are my 2 final classes:

class Class1 extends BaseClass
{
    ...
}

class Class2 extends BaseClass
{
    protected static $isCacheEnabled = true;
    protected static $cacheExpirationTime = 3600;
    ...
}

Here is the part of the code which executes these classes:

function baseClassRunner($baseClassName)
{
    ...
    $output = null;
    if ($baseClassName::isCacheEnabled()) {
        $output = Cache::getInstance()->get('the_key');
    }
    if ($output === null) {
        $baseClass = new $baseClassName();
        $output = $baseClass->getOutput();
        if ($baseClassName::isCacheEnabled()) {
            Cache::getInstance()->set('the_key', $output);
        }
    }
    ...
}

This code doesn't work because PHP complains about defining same properties in Class2 as in Cacheable. I can't set them in their constructors because I want to read them even before running the constructor. I'm open for ideas, any help would be appreciated. :)

EDIT:

Well, I use this Cacheable trait on several places so i kind of got mixed up. :) This works fine like this. But I have another class which directly uses the Cacheable trait and when I try to do this on that class, I get the metioned error. So... Just assume that the BaseClass isn't abstract and I'm trying to set these cache properties on it. The question remains the same.

Pentapody answered 30/11, 2013 at 10:56 Comment(0)
E
7

You can not reassign trait properties.

From PHP manual http://php.net/traits

See Example #12 Conflict Resolution

If a trait defines a property then a class can not define a property with the same name, otherwise an error is issued. It is an E_STRICT if the class definition is compatible (same visibility and initial value) or fatal error otherwise.

One solution would be to define override properties in the class

class Class2 extends BaseClass
{
    protected static $_isCacheEnabled = true;
    protected static $_cacheExpirationTime = 3600;
    ...
}

and then modify your trait as such...

trait Cacheable
{
    protected static $isCacheEnabled = false;
    protected static $cacheExpirationTime = null;

    public static function isCacheEnabled()
    {

        if ( Cache::isEnabled() ) {
            return isset( static::$_isCacheEnabled ) ? static::$_isCacheEnabled :
                static::$isCacheEnabled;
        } else {
            return false;
        }

    }

    public static function getCacheExpirationTime()
    {
        return isset ( static::$_cacheExpirationTime ) ? static::$_cacheExpirationTime :        
            static::$cacheExpirationTime;
    }
}
Errhine answered 7/12, 2013 at 4:50 Comment(2)
Yeah, but this is ugly, I have 2 properties for the same thing. :/Pentapody
Not 2 properties if you just have a default value in the statement isset( static::$_isCacheEnabled ) ? static::$_isCacheEnabled : false;Shevat
E
1

You cannot override properties, but you can override functions. So one of the possible solutions, if you're going to use the properties as given, not changing them, could be:

trait Cacheable {
    protected static function isCacheEnabledForClass() { return false; }

    public static function isCacheEnabled()
    {
        return static::isCacheEnabledForClass() && Cache::isEnabled();
    }
}


class Class2 extends BaseClass {
    protected static function isCacheEnabledForClass() { return true; }
}
Erine answered 17/1, 2018 at 7:3 Comment(0)
O
0

You could use defined():

// only defined in classes
// static $isCacheEnabled = false;

public static function isCacheEnabled()
{
    return defined(static::$isCacheEnabled ) ? static::$isCacheEnabled : false;
}

Or maybe you could live with the variable being protected instead of static?

Onestep answered 20/8, 2016 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.