【问题标题】:Many different classes in @return. How to avoid writing them all?@return 中有许多不同的类。如何避免全部写出来?
【发布时间】:2014-06-05 19:37:41
【问题描述】:

我有一个函数:

$model = $this->model('aNameOfTheModel');

除了这个之外,还有另一种 PHPDocing model() 的方法:

/**
 * @return A|B|C|D|E|F model(string $name)
 */

A、B、C...类有不同的方法(例如A::getMaleUsersB::whenPartyStarts)。我想避免在@return 中写所有 A、B、C...。

【问题讨论】:

  • 它们是否共享一个共同的超类型?如果没有,您可能会被mixed 卡住。
  • 它们都彼此不同。输入 mixed 将关闭 IDE 中的建议。
  • 我的超载王国。
  • 保留建议的唯一解决方案是将/** @var $model A */ 放在$model 上方。这很痛苦。

标签: php phpdoc


【解决方案1】:

通常,您会在每个类中定义一个model() 函数。然后你会调用那个类的模型函数。 (通常模型函数也是静态的,因为它们将对象实例化为单例,但我想这一切都取决于你在做什么)

然后您可以单独对每个类进行 PHPDoc,并通过扩展包含父模型函数的类来运行相同的底层代码。那么每个类里面的模型方法可能是这样的:

class foo extends bar {

  /** @return foo */
  public static function model()
  {
    return parent::model('foo');
  }

}

然后在你扩展的类中,你有一个像这样的模型函数:

class bar {

  public static function model($className)
  {
    //todo: code for whatever you do to get your model class object
    return $model
  }

}

【讨论】:

  • 在我的例子中model() 是一个Kernel 方法,它引导数据库(如果它是第一个model() 调用)然后创建模型。所以,很遗憾我无法实施您的建议。
猜你喜欢
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 2016-05-27
相关资源
最近更新 更多