我有两个类(class)。一个 child 和一个 parent 。父级正在从子级调用静态方法(这是子类中覆盖的静态父级方法),我收到一般服务器错误。 当我删除关系(“扩展”部分)时,一切都很好并且没有错误。不知道为什么。不能覆盖静态方法吗?寻找答案,但似乎找不到。
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) {
}
}
最佳答案
虽然这看起来很奇怪。基应该不知道派生。
也许改用 static::
并依靠覆盖静态成员函数 — 或“后期静态绑定(bind)”。为此,您需要 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);
?>
关于php - 从父级调用覆盖的静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6809191/