Heim  >  Artikel  >  Backend-Entwicklung  >  YII2抽象类BaseActiveRecord,实现接口ActiveRecordInterface,其find()方法没有重载!

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

WBOY
WBOYOriginal
2016-06-06 20:38:071060Durchsuche

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn