简体   繁体   中英

Calling an overriden static method from parent

I have two classes. A child and a parent. The parent is calling a static method from the child (that's an overriden static parent method in the child class) and I get a general server error. When I remove the relation ('extends' part), all is fine and get no errors. No idea why. Can't you override static methods? Looked for answers but can't seem to find them.

Class Fase {

  public static function getbyId($id) {
   //some stuff
      }
  public function getsomefaseitem($fase_item_id) {
     FaseItem::getbyid($fase_item_id);
    }

}

Class FaseItem extends Fase {

  public static function getbyId($id) {

      }
}

It works for me.


This seems weird, though. The base should have no knowledge of the derived.

Perhaps use static:: instead and rely on overriding static member functions — or "late static binding". You'll need PHP 5.3 for this.

<?php
class Fase {
   public static function getbyId($id) {
      echo "Fase::getbyId\n";
   }

   public function getsomefaseitem($fase_item_id) {
      static::getbyid($fase_item_id); // <---
   }
}

class FaseItem extends Fase {

   public static function getbyId($id) {
     echo "FaseItem::getbyId\n";
   }
}


$f = new Fase();
$f->getsomefaseitem(0);
?>

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