如何指定 PHPDoc 中继承的方法返回的对象的类型


How do I specify the type of an object returned by an inherited method in PHPDoc?

假设我有这个抽象数据库表类:

abstract class DbTable {
    /**
     * Table records
     * @var DatabaseRecordIF[] 
     */
   protected $records;
   /**
    * Returns the records.
    * @return DatabaseRecordIf[]
    */
   public function getRecords() {
       return $this->records;
   }
}

我有一个特定的数据库表类来存储表中的记录:

class MyTable extends DbTable { }

我有一个类在该表中定义一条记录:

class MyTableRecord implements DatabaseRecordIf { }

我想告诉 phpDocumentor MyTable::getRecords()返回MyTableRecord[],而不仅仅是DatabaseRecordIf[]。 这可能吗?

有没有更好的方法可以解决这个问题?

phpDocumentor 定义staticself,用于指定继承方法返回的类型。

self 使用此类型的类的对象,如果继承,它仍将表示最初定义它的类。

static使用此值的类的对象,如果继承,它将表示子类(请参阅 PHP 手册中的后期静态绑定)。

因此,静态是返回子类型而不是父类型所需的类型。

   /**
    * Returns the records.
    * @return static[]
    */
   public function getRecords() {
       return $this->records;
   }