简体   繁体   English

从父级调用重写静态方法

[英]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. 当我删除关系('extends'部分)时,一切都很好并且没有错误。 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". 也许使用static::而依赖于重写静态成员函数 - 或“后期静态绑定”。 You'll need PHP 5.3 for this. 你需要PHP 5.3。

<?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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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