首页 > 解决方案 > 注释:如何在 php 的 docblocks 中指定时间戳

问题描述

我有一个返回时间戳的方法。我想做这样的事情:

class MyAwesomeService {
    
    /**
     * @return array<int, timestamp>
     */
    public function myAwesomeMethod(): array
    {
        return [
            1636380000,    
            1636385555,
            1636386666,
        ];
    }
}

但是,我认为不@return array<int, timestamp>成立。

在文档块中指定时间戳的有效格式是什么?

标签: phpannotationsdocblocks

解决方案


您可以使用int[],时间戳没有有效值。但是,您可以创建一个 ValueObject。

class MyAwesomeService {
    
    /**
     * @return int[]
     */
    public function myAwesomeMethod(): array
    {
        return [
            1636380000,    
            1636385555,
            1636386666,
        ];
    }
}

如果您使用值对象:

final class Timestamp
{
    private $timestamp;
    public function __construct(int $timestamp) {
        $this->timestamp = $timestamp; 
    }
    
    public function get() : int 
    {
        return $this->timestamp;
    }
}

class MyAwesomeService {
    
    /**
     * @return Timestamp[]
     */
    public function myAwesomeMethod(): array
    {
        return [
            new Timestamp(1636380000),    
            new Timestamp(1636385555),
            new Timestamp(1636386666),
        ];
    }
}

推荐阅读