简体   繁体   中英

How to override static methods with non static methods?

How to override static methods with non static methods?

code

class Env {
    static protected $vars = [
    ];

    static public function get_env_var($var){
        return self::$vars[$var];
    }

    static public function set_env_var($var, $value){
        self::$vars[$var] = $value;
    }
}

class Test extends Env {
    private $env_vars = [];

    public function __construct(){
        $this->env_vars = self::$vars;
    }

    public function get_env_var($var){
        return $this->env_vars[$var];
    }

    public function set_env_var($var, $value){
        $this->env_vars[$var] = $value;
    }
}

$Test = new Test();

error

Cannot make static method Env::get_env_var() non static in class Test in

You can't.

If you defined them as static before, you had valid reasons.

Original question was how to override static method to non static. Your answer is not the answer for the question, because child is still static.

You can't override a static method with a non static.

public static function get_env_var($var)
class Parent
{
    public static function start()
    {
        echo 'Parent::start';
    }
}

class Child extends Parent
{
    public static function start()
    {
        parent::start();
        echo 'Child::start';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM