0

I have a class "A" containing a static method. Now I want a copy of that class(whether by extension or not) except I need that static method to become non-static in the new class.

Any ideas are welcome. Thanks in advance.

I tried extending but when redeclaring the method as non-static I got an error:

class A {
  protected $key = null;

  static function methodX($args) {
    // ...
  }
}

class B extends A {
  protected $key = "key";

  public function methodX($args) {
    // ...
    return $this->key;
  }
}

EurekA
  • 66
  • 6

2 Answers2

0
  1. The actual flavor of overriding is linked with an object. Overriding is when we pass a child class object to a parent class reference variable but the static methods are associated with class they are not linked with an object so we can’t override a static method

  2. The answer here is no. What is the reason? Well, because non-static member variables of a class always belong to an object – meaning that every object has it’s own personal copy of non-static member variables (also known as instance variables). And, static functions have no object to work with since they belong to the class as a whole. Remember that you can call a static function without an object – and for that exact reason a static function can not call instance variables.

  3. you can override only static method to static method.

Amarat
  • 602
  • 5
  • 11
0

But you can't, you have to override it as static or create a new function for non-static in your child.

JessGabriel
  • 1,062
  • 9
  • 18