YII2抽象类BaseActiveRecord,实现接口ActiveRecordInterface,其find()方法没有重载!


BaseActiveRecord类的文件路径为:项目\vendor\yiisoft\yii2\db\BaseActiveRecord.php
是因为BaseActiveRecord类为abstract才不需要实现接口中的全部方法,还是接口中的find()方法为static就不需要实现?还是yii2中再别的地方做了处理?

抽象类 php接口 php yii2

暂时没想到 11 years ago

php5.5.11测试了一下,发现abstract类不需要实现Interface中的方法,但是要在继承类中实现Interface中的所有方法。
BaseActiveRecord继承了model的部分方法,剩下的方法在继承类ActiveRecord中实现了。

ActiveRecordInterface.php


 php


 interface ActiveRecordInterface{
    public static function a();
    public function b();
    public function c();
}

BaseActiveRecord.php


 php


 include "ActiveRecordInterface.php";
abstract class BaseActiveRecord implements ActiveRecordInterface{
    // public static function a(){echo 'a';}
    // public function b(){echo 'b';}
    // public function c(){echo 'c';}
}

test.php


 php


 include "BaseActiveRecord.php";

class test extends BaseActiveRecord{
    public static function a(){}
    public function b(){}
    public function c(){}
    function d(){
        echo 'dd';
    }
}
$a = new test();
$a->d();

正常输出dd

中二病无法好转 answered 11 years ago

Your Answer