2011-01-22 34 views
1

以下是Base.php代码和类基地在phpDoc中,我如何编写一个我不知道还会返回的类?

/** 
* @return ? 
*/ 
public static function withId($id, $class = __CLASS__) 
{ 
    $instance = new $class($id); 
    if ($instance->getId() == self::ID_OF_UNKNOWN_USER) { 
     $instance = null; 
    } 
    return $instance; 
} 

其他类将扩展此类。我使用迟了的静态绑定来确定在调用withId()并将类名传递给$ class之前应该创建的对象。

返回的类可以是Base或其任何子类。我如何在phpDoc中标记?

回答

1

这看起来有点像工厂模式,在这种情况下,用户代码不应该知道返回的具体类。通常你会使用抽象类或接口和程序。在这种情况下:

/** 
* @return null|Base 
*/ 

有没有办法使用的文档字符串(这是静态的)在运行时生成的值。

0

返回的类可以是Base或其任何子级。我如何在phpDoc中标记?

直向前。

/** 
* @return Base 
*/ 
相关问题