一般来说,在 PHP 中使用 DocBlock 是最佳实践之一。它对于以前的 PHP 版本(小于 PHP 7.3 或特别是 7.4)非常有用。它通知开发人员有关类属性类型、预期参数类型和方法返回值(在 PHP 中缺乏严格类型的情况下)。
假设在 PHP 5.6 中,代码如下所示:
namespace App\Service\Catalog\Category;
use App\Entity\Catalog\Category\Category;
use App\Repository\Catalog\Category\CategoryRepository;
class CategoryService
{
/** @var CategoryRepository */
private $categoryRepository;
/** @var int */
private $currentNestingLevel = 1;
/**
* CategoryService constructor.
* @param CategoryRepository $categoryRepository
*/
public function __construct(Category $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
/**
* @param $parentCategoryId
* @return array
*/
public function getCategoriesDataByParentCategoryId($parentCategoryId)
{
$categories = $this->categoryRepository->getByParentCategoryId($parentCategoryId);
$categoriesData = [];
foreach ($categories as $category) …Run Code Online (Sandbox Code Playgroud)