Maison  >  Article  >  développement back-end  >  YII2抽象类BaseActiveRecord,实现接口ActiveRecordInterface,其find()方法没有重载!

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

WBOY
WBOYoriginal
2016-06-06 20:38:071060parcourir

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

回复内容:

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

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

ActiveRecordInterface.php

<code>php</code><code>interface ActiveRecordInterface{
    public static function a();
    public function b();
    public function c();
}
</code>

BaseActiveRecord.php

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

test.php

<code>php</code><code>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();
</code>

正常输出dd

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn