【发布时间】:2014-03-25 06:41:03
【问题描述】:
我在 php 中有一个示例代码
class First {
public static $name;
public static function getName() {
return static::$name;
}
}
class Second extends First {
public static $name = 'second';
}
echo Second::getName(); // print 'second'
但是当我将它写入fuelphp时:
文件 1:
namespace Model;
use \DB;
class ModelMain extends \Model {
public static $table_name;
public static function getName() {
return self::$table_name;
}
}
文件 2
class Post extends \Model\ModelMain {
public static $table_name = "post";
}
当我打电话时
Post::getName() // Print null
我预计它会打印帖子。它有什么问题?
【问题讨论】:
-
尝试用
return static::$table_name;替换return self::$table_name; -
好答案 :) 非常感谢
-
这是由于静态方法中缺乏多态行为。
-
@hoangvu68,你之前不是已经有答案了吗?
-
:) 抱歉,刚刚接受了您的回答