jeudi 14 janvier 2021

PHP Pattern for calling instrance methods inside static method [closed]

We inherited some legacy code that was originally written as a class with all static methods. There was never a reason for all the static methods, and they make testing very difficult, so we're in the process of removing static from many of them.

A couple methods do need to remain static and I'm wondering what is the best practice when those static methods need to call other not static methods.

We've been doing something like this:

public static function static_method() {
  $self = new self();
  $self->not_static_method();
} 

public function not_static_method() {
  return "foo";
}

Is there a better way?

Aucun commentaire:

Enregistrer un commentaire